Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to keep only the English alphabet in php

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "php how to retain only the English alphabet". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "php how to retain only the English alphabet" can help you solve your doubts.

In php, you can use preg_match_all () with regular expressions to filter strings, leaving only English letters; syntax "preg_match_all (" / [a-zA-Z] / u "," $str ", $arr)", letters are stored in an array, and you can use join () to convert an array into a string.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Php retains only the English alphabet

You can use the preg_match_all () function with regular expressions to filter strings, leaving only the English alphabet

The preg_match_all () function stores the matching characters (reserved alphabets) in an array (specified by the third argument).

As you can see, the result is a two-dimensional array.

If you want to use the join () function to concatenate the resulting value into a string, you need to use the following statement

Join ('', $arr [0])

Description: preg_match_all ()-performs global regular expression matching

The preg_match_all () function searches for all results in a string that match a regular expression, in the following syntax format:

Preg_match_all ($pattern, $subject [, & $matches [, $flags = PREG_PATTERN_ORDER [, $offset = 0])

The parameters are described as follows:

$pattern: the pattern to search for, that is, the defined regular expression

$subject: string to search for

$matches: optional parameter (multi-dimensional array) to store all matching results. Array sorting is specified by $flags

$flags: optional parameter that can be used in combination with the following tags (note that PREG_PATTERN_ORDER and PREG_SET_ORDER cannot be used at the same time):

PREG_PATTERN_ORDER: the result is sorted as $matches [0] to save all matches for the complete pattern, $matches [1] to save all matches for the first subgroup, and so on.

PREG_SET_ORDER: the order of the result is $matches [0] containing all matches (including subgroups) from the first match, $matches [1] is an array containing all matches (including subgroups) from the second match, and so on.

PREG_OFFSET_CAPTURE: if this tag is passed, each found match returns with an increase in its offset from the target string. Note that this changes each matching result string element in $matches so that the 0th element is the matching result string, and the first element is the offset of the matching result string in the subject.

$offset: optional parameter, $offset is used to start the search (in bytes) from the location specified in the target string.

The preg_match_all () function can return the number of matches for $pattern (possibly 0) and FALSE if an error occurs.

After reading this, the article "how to keep only the English alphabet in php" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report