wp_check_invalid_utf8() - WordPress 函数参考中文文档

返回介绍

wp_check_invalid_utf8()

发布于 2017-09-11 字数2445 浏览 1033 评论 0

wp_check_invalid_utf8( string $string,  bool $strip = false )

Checks for invalid UTF8 in a string.


描述


参数

$string

(string)
(Required)
The text which is to be checked.

$strip

(bool)
(Optional)
Whether to attempt to strip out invalid UTF8. Default is false.

Default value: false


返回值

(string) The checked text.


源代码

File: wp-includes/formatting.php

function wp_check_invalid_utf8( $string, $strip = false ) {
	$string = (string) $string;

	if ( 0 === strlen( $string ) ) {
		return '';
	}

	// Store the site charset as a static to avoid multiple calls to get_option()
	static $is_utf8 = null;
	if ( ! isset( $is_utf8 ) ) {
		$is_utf8 = in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) );
	}
	if ( ! $is_utf8 ) {
		return $string;
	}

	// Check for support for utf8 in the installed PCRE library once and store the result in a static
	static $utf8_pcre = null;
	if ( ! isset( $utf8_pcre ) ) {
		$utf8_pcre = @preg_match( '/^./u', 'a' );
	}
	// We can't demand utf8 in the PCRE installation, so just return the string in those cases
	if ( !$utf8_pcre ) {
		return $string;
	}

	// preg_match fails when it encounters invalid UTF8 in $string
	if ( 1 === @preg_match( '/^./us', $string ) ) {
		return $string;
	}

	// Attempt to strip the bad chars if requested (not recommended)
	if ( $strip && function_exists( 'iconv' ) ) {
		return iconv( 'utf-8', 'utf-8', $string );
	}

	return '';
}

更新日志

Version 描述
2.8.0 Introduced.

相关函数

Uses

  • wp-includes/option.php:
    get_option()

Used By

  • wp-includes/formatting.php:
    _sanitize_text_fields()
  • wp-includes/formatting.php:
    esc_js()
  • wp-includes/formatting.php:
    esc_html()
  • wp-includes/formatting.php:
    esc_attr()

User Contributed Notes

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!