How to keep only English letters in php

青灯夜游
Release: 2023-03-15 21:22:01
Original
2925 people have browsed it

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.

How to keep only English letters in php

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);
?>
Copy after login

How to keep only English letters in php

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(&#39;&#39;,$arr[0])
Copy after login

How to keep only English letters in php

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 ]]])
Copy after login

Parameter description as follows:

  • $pattern: The pattern to be searched, which is the defined regular expression;
  • $subject: the string to be searched;
  • $matches: Optional parameter (multidimensional array), used to store all matching results. Array sorting is specified by $flags;
  • $flags: optional parameters, can be used in combination with the following flags (note that PREG_PATTERN_ORDER and PREG_SET_ORDER cannot be used at the same time):
    • PREG_PATTERN_ORDER: The results are ordered so that $matches[0] holds all matches for the complete pattern, $matches[1] holds all matches for the first subgroup, and so on.
    • PREG_SET_ORDER: The results are sorted as $matches[0] contains all matches (including subgroups) obtained by the first match, $matches[1] is an array containing all matches (including subgroups) obtained by the second match, so that analogy.
    • PREG_OFFSET_CAPTURE: If this flag is passed, each found match is returned with its offset relative to the target string increased. Note that this will change each match string element in $matches to be one where the 0th element is the match string and the 1st element is the offset of the match string within the subject.
  • $offset: Optional parameter, $offset is used to start searching from the specified position in the target string (unit is byte).

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template