esc_js

函式
esc_js ( $text )
引數
  • (string) $text The text to be escaped.
    Required:
返回值
  • (string) Escaped text.
定義位置
相關方法
esc_sqlesc_urlesc_xmlesc_attresc_html
引入
2.8.0
棄用
-

esc_js: 這個函式用於轉義一個字串,以便在JavaScript程式碼中使用。

消除單引號、`"`、`<`、`>`、`&`,並修復行尾。

在JS中對文字字串進行轉義。它旨在用於內聯JS(在一個標籤屬性中,例如`onclick=””…””)。注意,字串必須在單引號中。{@see ‘js_escape’}過濾器也適用於此。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 );
}
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 ); }
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 );
}

常見問題

FAQs
檢視更多 >