wpdb::check_ascii()protectedWP 4.2.0

Checks if a string is ASCII.

The negative regex is faster for non-ASCII strings, as it allows the search to finish as soon as it encounters a non-ASCII character.

Method of the class: wpdb{}

No Hooks.

Return

true|false. True if ASCII, false if not.

Usage

// protected - for code of main (parent) or child class
$result = $this->check_ascii( $input_string );
$input_string(string) (required)
String to check.

Changelog

Since 4.2.0 Introduced.

wpdb::check_ascii() code WP 6.5.2

protected function check_ascii( $input_string ) {
	if ( function_exists( 'mb_check_encoding' ) ) {
		if ( mb_check_encoding( $input_string, 'ASCII' ) ) {
			return true;
		}
	} elseif ( ! preg_match( '/[^\x00-\x7F]/', $input_string ) ) {
		return true;
	}

	return false;
}