wp_remove_surrounding_empty_script_tags

函数
wp_remove_surrounding_empty_script_tags ( $contents )
参数
  • (string) $contents Script body with manually created SCRIPT tag literals.
    Required:
返回值
  • (string) Script body without surrounding script tag literals, or original contents if both exact literals aren't present.
相关
  • wp_print_inline_script_tag()
  • wp_get_inline_script_tag()
定义位置
相关方法
wp_print_script_tagwp_get_script_tagwp_get_inline_script_tag__return_empty_stringremove_rewrite_tag
引入
6.4.0
弃用
-

删除前导和尾部 _empty_ script  标记。

这是一个辅助工具,用于在 ` wp_get_inline_script_tag() ` 或 ` wp_print_inline_script_tag() ` 中构建字面脚本标记。
它可以在删除内联脚本周围的 """" 字面值后修剪空白。通常,它与输出缓冲结合使用,其中 ` ob_get_clean() ` 作为 ` $contents ` 参数传递。

示例:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// Strips exact literal empty SCRIPT tags.
$js = 'sayHello();;
'sayHello();' === wp_remove_surrounding_empty_script_tags( $js );
// Otherwise if anything is different it warns in the JS console.
$js = 'console.log( "hi" );';
'console.error( ... )' === wp_remove_surrounding_empty_script_tags( $js );
// Strips exact literal empty SCRIPT tags. $js = 'sayHello();; 'sayHello();' === wp_remove_surrounding_empty_script_tags( $js ); // Otherwise if anything is different it warns in the JS console. $js = 'console.log( "hi" );'; 'console.error( ... )' === wp_remove_surrounding_empty_script_tags( $js );
// Strips exact literal empty SCRIPT tags.
$js = 'sayHello();;
'sayHello();' === wp_remove_surrounding_empty_script_tags( $js );

// Otherwise if anything is different it warns in the JS console.
$js = 'console.log( "hi" );';
'console.error( ... )' === wp_remove_surrounding_empty_script_tags( $js );

 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_remove_surrounding_empty_script_tags( $contents ) {
$contents = trim( $contents );
$opener = '<SCRIPT>';
$closer = '</SCRIPT>';
if (
strlen( $contents ) > strlen( $opener ) + strlen( $closer ) &&
strtoupper( substr( $contents, 0, strlen( $opener ) ) ) === $opener &&
strtoupper( substr( $contents, -strlen( $closer ) ) ) === $closer
) {
return substr( $contents, strlen( $opener ), -strlen( $closer ) );
} else {
$error_message = __( 'Expected string to start with script tag (without attributes) and end with script tag, with optional whitespace.' );
_doing_it_wrong( __FUNCTION__, $error_message, '6.4' );
return sprintf( 'console.error(%s)', wp_json_encode( __( 'Function wp_remove_surrounding_empty_script_tags() used incorrectly in PHP.' ) . ' ' . $error_message ) );
}
}
function wp_remove_surrounding_empty_script_tags( $contents ) { $contents = trim( $contents ); $opener = '<SCRIPT>'; $closer = '</SCRIPT>'; if ( strlen( $contents ) > strlen( $opener ) + strlen( $closer ) && strtoupper( substr( $contents, 0, strlen( $opener ) ) ) === $opener && strtoupper( substr( $contents, -strlen( $closer ) ) ) === $closer ) { return substr( $contents, strlen( $opener ), -strlen( $closer ) ); } else { $error_message = __( 'Expected string to start with script tag (without attributes) and end with script tag, with optional whitespace.' ); _doing_it_wrong( __FUNCTION__, $error_message, '6.4' ); return sprintf( 'console.error(%s)', wp_json_encode( __( 'Function wp_remove_surrounding_empty_script_tags() used incorrectly in PHP.' ) . ' ' . $error_message ) ); } }
function wp_remove_surrounding_empty_script_tags( $contents ) {
	$contents = trim( $contents );
	$opener   = '<SCRIPT>';
	$closer   = '</SCRIPT>';

	if (
		strlen( $contents ) > strlen( $opener ) + strlen( $closer ) &&
		strtoupper( substr( $contents, 0, strlen( $opener ) ) ) === $opener &&
		strtoupper( substr( $contents, -strlen( $closer ) ) ) === $closer
	) {
		return substr( $contents, strlen( $opener ), -strlen( $closer ) );
	} else {
		$error_message = __( 'Expected string to start with script tag (without attributes) and end with script tag, with optional whitespace.' );
		_doing_it_wrong( __FUNCTION__, $error_message, '6.4' );
		return sprintf( 'console.error(%s)', wp_json_encode( __( 'Function wp_remove_surrounding_empty_script_tags() used incorrectly in PHP.' ) . ' ' . $error_message ) );
	}
}

常见问题

FAQs
查看更多 >