In PHP, you can use preg_match_all() with regular expressions to filter strings and only retain English letters; the syntax is "preg_match_all("/[a-zA-Z]/u","$str", $arr)", the letters will be stored in the array, and join() can be used to convert the array into a string.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php only retains English The letters
can be used with the preg_match_all() function in conjunction with regular expressions to filter strings and only retain English letters
The preg_match_all() function will match the characters (reserved English letters) are stored in the array one by one (the array is specified by the third parameter).
<?php header("Content-type:text/html;charset=utf-8"); $str = "php.cn23v457zblog,?#$%^&())*(&^"; preg_match_all("/[a-zA-Z]/u","$str",$arr); var_dump($arr); ?>
It can be seen that the result is a two-dimensional array.
If you want to use the join() function to splice the result value into a string, you need to use the following statement
join('',$arr[0])
Instructions: preg_match_all()--Perform global regular expression matching
preg_match_all() function can search for all results in the string that can match the regular expression. The syntax format is as follows:
preg_match_all($pattern, $subject [, &$matches [, $flags = PREG_PATTERN_ORDER [, $offset = 0 ]]])
Parameter description as follows:
The preg_match_all() function can return the number of matches for $pattern (possibly 0), or FALSE if an error occurs.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to keep only English letters in php. For more information, please follow other related articles on the PHP Chinese website!