函数原型
esc_js( string $text ): string
函数描述
Escapes single quotes, ", , &, and fixes line endings.
是否弃用
未弃用
函数参数
-
$text
string
Required - The text to be escaped.
函数返回值
string Escaped text.
函数位置
File: wp-includes/formatting.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 );
}
源码链接
变更日志
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |

