wpdb::check_ascii( string $input_string ): bool

Checks if a string is ASCII.

Description

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.

Parameters

$input_stringstringrequired
String to check.

Return

bool True if ASCII, false if not.

Source

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;
}

Changelog

VersionDescription
4.2.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.