sanitize_user

函式
sanitize_user ( $username, $strict = false )
引數
  • (string) $username The username to be sanitized.
    Required:
  • (bool) $strict Optional. If set limits $username to specific characters. Default false.
    Required:
    Default: false
返回值
  • (string) The sanitized username, after passing through filters.
定義位置
相關方法
sanitize_urlsanitize_termsanitize_user_fieldsanitize_keysanitize_user_object
引入
2.0.0
棄用
-

sanitize_user: 這是一個WordPress的函式,它對一個使用者物件進行淨化。它用於驗證和淨化使用者的資料,如使用者名稱、密碼和電子郵件: 這個函式有一個引數,就是要淨化的使用者物件。

對使用者名稱進行淨化,刪除不安全的字元。

刪除標籤、八位數、實體,如果啟用了strict,將只保留字母數字、_、空格、.、-、@。淨化後,它將使用者名稱、原始使用者名稱(引數中的使用者名稱)和$strict的值作為{@see ‘sanitize_user’}過濾器的引數。

function sanitize_user( $username, $strict = false ) {
	$raw_username = $username;
	$username     = wp_strip_all_tags( $username );
	$username     = remove_accents( $username );
	// Kill octets.
	$username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '', $username );
	// Kill entities.
	$username = preg_replace( '/&.+?;/', '', $username );

	// If strict, reduce to ASCII for max portability.
	if ( $strict ) {
		$username = preg_replace( '|[^a-z0-9 _.-@]|i', '', $username );
	}

	$username = trim( $username );
	// Consolidate contiguous whitespace.
	$username = preg_replace( '|s+|', ' ', $username );

	/**
	 * Filters a sanitized username string.
	 *
	 * @since 2.0.1
	 *
	 * @param string $username     Sanitized username.
	 * @param string $raw_username The username prior to sanitization.
	 * @param bool   $strict       Whether to limit the sanitization to specific characters.
	 */
	return apply_filters( 'sanitize_user', $username, $raw_username, $strict );
}

常見問題

FAQs
檢視更多 >