逃避单引号,htmlspecialchar“
原型
esc_js( string $text )
描述
转义文本字符串以在JS中回显。它旨在用于内联JS(在标记属性中,例如onclick =“…”)。请注意,字符串必须使用单引号。 ‘js_escape’过滤器也适用于此处。
参数
$text
(string)
(Required)
要转义的文字。
返回值
(string)
转义文本。
源文件
路径:wp-includes/formatting.php
<?php
...
function esc_js( $text ) {
$safe_text = wp_check_invalid_utf8( $text );
$safe_text = _wp_specialchars( $safe_text, ENT_COMPAT );
$safe_text = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes( $safe_text ) );
$safe_text = str_replace( "r", '', $safe_text );
$safe_text = str_replace( "n", '\n', addslashes( $safe_text ) );
/**
* Filters a string cleaned and escaped for output in JavaScript.
*
* Text passed to esc_js() is stripped of invalid or special characters,
* and properly slashed for output.
*
* @since 2.0.6
*
* @param string $safe_text The text after it has been escaped.
* @param string $text The text prior to being escaped.
*/
return apply_filters( 'js_escape', $safe_text, $text );
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/esc_js/