In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how php detects whether there are only letters in a string". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope this article "how to detect whether there are only letters in a string by php" can help you solve the problem.
In php, the preg_match () function can be used with the regular expression "/ ^ [a-zA-Z\ s] + $/" to detect whether the string has only letters, the syntax "preg_match (" / ^ [a-zA-Z\ s] + $/ ", string)"; if the return value is 1, there are only letters, and if 0 is returned, it contains other characters.
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Php detects whether a string contains only letters. In other words, it determines whether a string is a pure English letter.
In php, you can use the preg_match () function with regular expressions to check.
Regular expression used: "/ ^ [a-zA-Z\ s] + $/"
The preg_match () function can search and match strings based on regular expressions. The syntax format of the function is as follows:
Preg_match ($pattern,$subject [, & $matches [, $flags = 0 [, $offset = 0])
The parameters are described as follows:
$pattern: the pattern to search for, that is, the edited regular expression
$subject: string to search for
$matches: optional parameter (array type), which will be populated with search results if $matches is provided. $matches [0] contains the text to which the full pattern matches, $matches [1] contains the text matched by the first capture subgroup, and so on
$flags: optional parameter. $flags can be set to PREG_OFFSET_CAPTURE. If this flag is passed, the string offset (relative to the target string) will be appended to the return for each occurrence of the match.
$offset: optional parameter that specifies where to start the search (in bytes) in the target string.
The preg_match () function can return the number of matches for $pattern, and its value will be 0 (mismatch) or 1, because preg_match () will stop searching after the first match.
If the return value is 1, there are only letters, and a return value of 0 contains other characters.
Example:
This kind of output is not very good. Let's customize a function and do a processing. Only letters will return true, otherwise false will be returned.
Function is_english ($str) {if (preg_match ("/ ^ [a-zA-Z\ s] + $/", $str)) {return true;} return false;}
Call the function to determine whether the string is made up of pure English letters
Var_dump (is_english ('php.cn')); / / bool (false) contains punctuation marks so returns false var_dump (is_english (' phpcn')); / / bool (true) var_dump (is_english ('phpcn123')); / / bool (false) contains 123var_dump (is_english (' Welcome here')); / / bool (false) is all in Chinese
That's all for "how php detects whether there are only letters in a string". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.