_sanitize_text_fields

函式
_sanitize_text_fields ( $str, $keep_newlines = false )
Access
Private
引數
  • (string) $str String to sanitize.
    Required:
  • (bool) $keep_newlines Optional. Whether to keep newlines. Default: false.
    Required:
    Default: false
返回值
  • (string) Sanitized string.
定義位置
相關方法
sanitize_text_fieldsanitize_textarea_fieldsanitize_term_fieldsanitize_post_fieldsanitize_user_field
引入
4.7.0
棄用
-

_sanitize_text_fields: 這個函式用於通過刪除任何HTML標籤和其他潛在的危險字元來對文字欄位進行淨化。它被用來對錶格和其他使用者輸入的資訊進行淨化。

內部輔助函式,對來自使用者輸入或資料庫的字串進行淨化處理。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function _sanitize_text_fields( $str, $keep_newlines = false ) {
if ( is_object( $str ) || is_array( $str ) ) {
return '';
}
$str = (string) $str;
$filtered = wp_check_invalid_utf8( $str );
if ( strpos( $filtered, '<' ) !== false ) {
$filtered = wp_pre_kses_less_than( $filtered );
// This will strip extra whitespace for us.
$filtered = wp_strip_all_tags( $filtered, false );
// Use HTML entities in a special case to make sure no later
// newline stripping stage could lead to a functional tag.
$filtered = str_replace( "<n", "&lt;n", $filtered );
}
if ( ! $keep_newlines ) {
$filtered = preg_replace( '/[rnt ]+/', ' ', $filtered );
}
$filtered = trim( $filtered );
$found = false;
while ( preg_match( '/%[a-f0-9]{2}/i', $filtered, $match ) ) {
$filtered = str_replace( $match[0], '', $filtered );
$found = true;
}
if ( $found ) {
// Strip out the whitespace that may now exist after removing the octets.
$filtered = trim( preg_replace( '/ +/', ' ', $filtered ) );
}
return $filtered;
}
function _sanitize_text_fields( $str, $keep_newlines = false ) { if ( is_object( $str ) || is_array( $str ) ) { return ''; } $str = (string) $str; $filtered = wp_check_invalid_utf8( $str ); if ( strpos( $filtered, '<' ) !== false ) { $filtered = wp_pre_kses_less_than( $filtered ); // This will strip extra whitespace for us. $filtered = wp_strip_all_tags( $filtered, false ); // Use HTML entities in a special case to make sure no later // newline stripping stage could lead to a functional tag. $filtered = str_replace( "<n", "&lt;n", $filtered ); } if ( ! $keep_newlines ) { $filtered = preg_replace( '/[rnt ]+/', ' ', $filtered ); } $filtered = trim( $filtered ); $found = false; while ( preg_match( '/%[a-f0-9]{2}/i', $filtered, $match ) ) { $filtered = str_replace( $match[0], '', $filtered ); $found = true; } if ( $found ) { // Strip out the whitespace that may now exist after removing the octets. $filtered = trim( preg_replace( '/ +/', ' ', $filtered ) ); } return $filtered; }
function _sanitize_text_fields( $str, $keep_newlines = false ) {
	if ( is_object( $str ) || is_array( $str ) ) {
		return '';
	}

	$str = (string) $str;

	$filtered = wp_check_invalid_utf8( $str );

	if ( strpos( $filtered, '<' ) !== false ) {
		$filtered = wp_pre_kses_less_than( $filtered );
		// This will strip extra whitespace for us.
		$filtered = wp_strip_all_tags( $filtered, false );

		// Use HTML entities in a special case to make sure no later
		// newline stripping stage could lead to a functional tag.
		$filtered = str_replace( "<n", "&lt;n", $filtered );
	}

	if ( ! $keep_newlines ) {
		$filtered = preg_replace( '/[rnt ]+/', ' ', $filtered );
	}
	$filtered = trim( $filtered );

	$found = false;
	while ( preg_match( '/%[a-f0-9]{2}/i', $filtered, $match ) ) {
		$filtered = str_replace( $match[0], '', $filtered );
		$found    = true;
	}

	if ( $found ) {
		// Strip out the whitespace that may now exist after removing the octets.
		$filtered = trim( preg_replace( '/ +/', ' ', $filtered ) );
	}

	return $filtered;
}

常見問題

FAQs
檢視更多 >