wp_strip_all_tags()
云策文档标注
概述
wp_strip_all_tags() 是 WordPress 核心函数,用于彻底移除字符串中的所有 HTML 标签,包括 'script' 和 'style' 标签及其内容。与 strip_tags() 不同,它会删除标签内的内容,确保返回纯文本字符串。
关键要点
- 函数签名:wp_strip_all_tags( $text, $remove_breaks = false ),其中 $text 为必需参数(字符串),$remove_breaks 为可选参数(布尔值,默认 false)。
- 返回处理后的字符串,如果输入非标量或 null,会触发警告并返回空字符串。
- 默认应用于多个过滤器,如 pre_comment_author_url、user_url 等,用于在评论、用户 URL 等场景自动清理 HTML。
- 内部使用正则表达式移除 'script' 和 'style' 标签内容,然后调用 strip_tags() 移除其他标签,可选移除换行和空格。
代码示例
$html = 'I am not strong';
var_dump($html);
// 输出 'I am not strong'
var_dump(wp_strip_all_tags($html));
// 输出 'I am not strong'注意事项
- 不能通过简单比较原始字符串和处理后字符串来判断文本是否为 HTML,因为函数返回前会应用 trim(),可能导致误判。建议在比较前对原始字符串也进行 trim() 处理。
原文内容
Properly strips all HTML tags including ‘script’ and ‘style’.
Description
This differs from strip_tags() because it removes the contents of the <script> and <style> tags. E.g. strip_tags( '<script>something</script>' ) will return ‘something’. wp_strip_all_tags() will return an empty string.
Parameters
$textstringrequired-
String containing HTML tags
$remove_breaksbooloptional-
Whether to remove left over line breaks and white space chars
Default:
false
Source
function wp_strip_all_tags( $text, $remove_breaks = false ) {
if ( is_null( $text ) ) {
return '';
}
if ( ! is_scalar( $text ) ) {
/*
* To maintain consistency with pre-PHP 8 error levels,
* wp_trigger_error() is used to trigger an E_USER_WARNING,
* rather than _doing_it_wrong(), which triggers an E_USER_NOTICE.
*/
wp_trigger_error(
'',
sprintf(
/* translators: 1: The function name, 2: The argument number, 3: The argument name, 4: The expected type, 5: The provided type. */
__( 'Warning: %1$s expects parameter %2$s (%3$s) to be a %4$s, %5$s given.' ),
__FUNCTION__,
'#1',
'$text',
'string',
gettype( $text )
),
E_USER_WARNING
);
return '';
}
$text = preg_replace( '@<(script|style)[^>]*?>.*?<!--\1-->@si', '', $text );
$text = strip_tags( $text );
if ( $remove_breaks ) {
$text = preg_replace( '/[rnt ]+/', ' ', $text );
}
return trim( $text );
}
Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |
Skip to note 3 content
Codex
Example
$html = '<strong>I am not strong</strong>'; var_dump($html); //output '<strong>I am not strong</strong>' var_dump(wp_strip_all_tags($html)); //output 'I am not strong'Skip to note 4 content
Giuseppe
Be aware that
wp_strip_all_tags()cannot be used to check if a text is html by a simple comparison between the original string and the parsed string.The difference is in the
trim()applied to the returned value.function wrong_is_html( $my_string ) { return wp_strip_all_tags( $my_string ) !== $my_string ? true : false; } function is_html( $my_string ) { return wp_strip_all_tags( $my_string ) !== trim ( $my_string ) ? true : false; } $a_string = 'this is a simple text string with no html tag, but with an end of line' . PHP_EOL; if ( true === wrong_is_html( $a_string ) ) { echo 'Is HTML'; } else { echo 'Is not HTML'; } if ( true === is_html( $a_string ) ) { echo 'Is HTML'; } else { echo 'Is not HTML'; } // outputs // Is HTML // Is not HTML