php PHP正则表达式。检查字符串是否只包含字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2703785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:24:54  来源:igfitidea点击:

PHP Regular Expression. Check if String contains ONLY letters

phpregexstring

提问by user69514

In PHP, how do I check if a String contains only letters? I want to write an if statement that will return false if there is (white space, number, symbol) or anything else other than a-zand A-Z.

在 PHP 中,如何检查字符串是否仅包含字母?我想编写一个 if 语句,如果有(空格、数字、符号)或除a-zand之外的任何其他内容,则该语句将返回 false A-Z

My string must contain ONLY letters.

我的字符串必须只包含字母。

I thought I could do it this way, but I'm doing it wrong:

我以为我可以这样做,但我做错了:

if( ereg("[a-zA-Z]+", $myString))
   return true;
else
   return false;

How do I find out if myStringcontains only letters?

如何确定是否myString只包含字母?

回答by user69514

Yeah this works fine. Thanks

是的,这很好用。谢谢

if(myString.matches("^[a-zA-Z]+$"))

回答by Carl Smotricz

Never heard of ereg, but I'd guess that it will match on substrings.

从未听说过ereg,但我猜它会匹配子字符串。

In that case, you want to include anchors on either end of your regexp so as to force a match on the whole string:

在这种情况下,您希望在正则表达式的任一端包含锚点,以强制匹配整个字符串:

"^[a-zA-Z]+$"

Also, you could simplify your function to read

此外,您可以简化您的功能以阅读

return ereg("^[a-zA-Z]+$", $myString);

because the ifto return trueor falsefrom what's already a boolean is redundant.

因为ifto returntruefalsefrom what has been a boolean 是多余的。



Alternatively, you could match on any character that's nota letter, and return the complement of the result:

或者,您可以匹配任何不是字母的字符,并返回结果的补码:

return !ereg("[^a-zA-Z]", $myString);

Note the ^at the beginning of the character set, which inverts it. Also note that you no longer need the +after it, as a single "bad" character will cause a match.

请注意^字符集开头的 ,将其反转。另请注意,您不再需要它+之后,因为单个“坏”字符将导致匹配。



Finally... this advice is for Java because you have a Javatag on your question. But the $in $myStringmakes it look like you're dealing with, maybe Perl or PHP? Some clarification might help.

最后……这个建议是针对 Java 的,因为你Java的问题有一个标签。但是$in$myString使它看起来像您正在处理的,也许是 Perl 或 PHP?一些澄清可能会有所帮助。

回答by codaddict

Your code looks like PHP. It would return trueif the string has a letter in it. To make sure the string has onlyletters you need to use the start and end anchors:

您的代码看起来像PHP. 这将返回true如果字符串中有一个字母。要确保字符串只有字母,您需要使用开始和结束锚点

In Javayou can make use of the matchesmethod of the Stringclass:

Java你可以利用的matches的方法String类:

boolean hasOnlyLetters(String str) {
   return str.matches("^[a-zA-Z]+$");
}

In PHPthe function eregis deprecatednow. You need to use the preg_matchas replacement. The PHPequivalent of the above function is:

PHP函数ereg中现在已弃用。您需要使用preg_match作为替换。PHP上述函数的等价物是:

function hasOnlyLetters($str) {
   return preg_match('/^[a-z]+$/i',$str);
}

回答by polygenelubricants

I'm going to be different and use Character.isLetterdefinition of what is a letter.

我将有所不同,并使用Character.isLetter什么是字母的定义。

if (myString.matches("\p{javaLetter}*"))

Note that this matches more than just [A-Za-z]*.

请注意,这不仅仅是匹配[A-Za-z]*.

A character is considered to be a letter if its general category type, provided by Character.getType(ch), is any of the following: UPPERCASE_LETTER, LOWERCASE_LETTER, TITLECASE_LETTER, MODIFIER_LETTER, OTHER_LETTER

Not all letters have case. Many characters are letters but are neither uppercase nor lowercase nor titlecase.

如果字符由 提供的一般类别类型Character.getType(ch)为以下任何一种,则该字符被视为字母:UPPERCASE_LETTER、LOWERCASE_LETTER、TITLECASE_LETTER、MODIFIER_LETTER、OTHER_LETTER

并非所有字母都有大小写。许多字符是字母,但既不是大写也不是小写也不是标题。

The \p{javaXXX}character classes is defined in PatternAPI.

\p{javaXXX}字符类中定义Pattern的API

回答by Anonymoose

Alternatively, try checking if it contains anything other than letters: [^A-Za-z]

或者,尝试检查它是否包含字母以外的任何内容:[^A-Za-z]

回答by Thorbj?rn Ravn Andersen

The easiest way to do a "is ALL characters of a given type" is to check if ANY character is NOT of the type.

执行“是给定类型的所有字符”的最简单方法是检查是否有任何字符不属于该类型。

So if \W denotes a non-character, then just check for one of those.

因此,如果 \W 表示非字符,则只需检查其中一个。