wp_specialchars_decode

函式
wp_specialchars_decode ( $string, $quote_style = ENT_NOQUOTES )
引數
  • (string) $string The text which is to be decoded.
    Required:
  • (string|int) $quote_style Optional. Converts double quotes if set to ENT_COMPAT, both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES. Also compatible with old _wp_specialchars() values; converting single quotes if set to 'single', double if set to 'double' or both if otherwise set. Default is ENT_NOQUOTES.
    Required:
    Default: ENT_NOQUOTES
返回值
  • (string) The decoded text without HTML entities.
定義位置
相關方法
wp_specialchars_wp_specialcharswp_json_file_decodewp_cache_decrwp_cache_close
引入
2.8.0
棄用
-

wp_specialchars_decode: 這個函式與wp_specialchars相反,它將特殊的HTML實體轉換為字元。它接收一個用特殊字元編碼的字串,並對其進行解碼,使其可以作為正常字元顯示。

將一些HTML實體轉換為它們的特殊字元。

特別是處理。`&`, `, `””, 和 `’`.

`$quote_style`可以設定為ENT_COMPAT來解碼`””`實體,或者ENT_QUOTES來解碼`””`和`’`。預設是ENT_NOQUOTES,不對引號進行解碼。

function wp_specialchars_decode( $string, $quote_style = ENT_NOQUOTES ) {
	$string = (string) $string;

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

	// Don't bother if there are no entities - saves a lot of processing.
	if ( strpos( $string, '&' ) === false ) {
		return $string;
	}

	// Match the previous behavior of _wp_specialchars() when the $quote_style is not an accepted value.
	if ( empty( $quote_style ) ) {
		$quote_style = ENT_NOQUOTES;
	} elseif ( ! in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) {
		$quote_style = ENT_QUOTES;
	}

	// More complete than get_html_translation_table( HTML_SPECIALCHARS ).
	$single      = array(
		''' => ''',
		''' => ''',
	);
	$single_preg = array(
		'/&#0*39;/'   => ''',
		'/&#x0*27;/i' => ''',
	);
	$double      = array(
		'"' => '"',
		'"' => '"',
		'"' => '"',
	);
	$double_preg = array(
		'/&#0*34;/'   => '"',
		'/&#x0*22;/i' => '"',
	);
	$others      = array(
		'&lt;'   => '<',
		'<' => '<',
		'&gt;'   => '>',
		'>' => '>',
		'&amp;'  => '&',
		'&' => '&',
		'&' => '&',
	);
	$others_preg = array(
		'/&#0*60;/'   => '<',
		'/&#0*62;/'   => '>',
		'/&#0*38;/'   => '&',
		'/&#x0*26;/i' => '&',
	);

	if ( ENT_QUOTES === $quote_style ) {
		$translation      = array_merge( $single, $double, $others );
		$translation_preg = array_merge( $single_preg, $double_preg, $others_preg );
	} elseif ( ENT_COMPAT === $quote_style || 'double' === $quote_style ) {
		$translation      = array_merge( $double, $others );
		$translation_preg = array_merge( $double_preg, $others_preg );
	} elseif ( 'single' === $quote_style ) {
		$translation      = array_merge( $single, $others );
		$translation_preg = array_merge( $single_preg, $others_preg );
	} elseif ( ENT_NOQUOTES === $quote_style ) {
		$translation      = $others;
		$translation_preg = $others_preg;
	}

	// Remove zero padding on numeric entities.
	$string = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $string );

	// Replace characters according to translation table.
	return strtr( $string, $translation );
}

常見問題

FAQs
檢視更多 >