[英]PHP Regular Expression. Check if String contains ONLY letters
In PHP, how do I check if a String contains only letters? 在PHP中,如何检查String是否只包含字母? I want to write an if statement that will return false if there is (white space, number, symbol) or anything else other than az
and AZ
. 我想写一个if语句,如果有(空格,数字,符号)或除az
和AZ
之外的任何其他内容,将返回false。
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 myString
contains only letters? 如何确定myString
是否只包含字母?
Yeah this works fine. 是的,这很好。 Thanks 谢谢
if(myString.matches("^[a-zA-Z]+$"))
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 if
to return true
or false
from what's already a boolean is redundant. 因为if
从已经是布尔值返回true
或false
是多余的。
Alternatively, you could match on any character that's not a 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 Java tag on your question. 最后......这个建议是针对Java的,因为你的问题上有一个Java标记。 But the $
in $myString
makes it look like you're dealing with, maybe Perl or PHP? 但是, $
在$myString
使它看起来像你正在处理的,也许Perl或PHP? Some clarification might help. 一些澄清可能会有所帮助。
Your code looks like PHP
. 您的代码看起来像PHP
。 It would return true
if the string has a letter in it. 如果字符串中有一个字母,它将返回true
。 To make sure the string has only letters you need to use the start and end anchors : 要确保字符串只有字母,您需要使用开始和结束锚点 :
In Java
you can make use of the matches
method of the String
class: 在Java
您可以使用String
类的matches
方法:
boolean hasOnlyLetters(String str) {
return str.matches("^[a-zA-Z]+$");
}
In PHP
the function ereg
is deprecated now. 在PHP
,函数ereg
现在已弃用 。 You need to use the preg_match
as replacement. 您需要使用preg_match
作为替换。 The PHP
equivalent of the above function is: 上述函数的PHP
等价物是:
function hasOnlyLetters($str) {
return preg_match('/^[a-z]+$/i',$str);
}
I'm going to be different and use Character.isLetter
definition 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 如果Character.getType(ch)
提供的常规类别类型是以下任何一种字符,则该字符被视为字母:UPPERCASE_LETTER,LOWERCASE_LETTER,TITLECASE_LETTER,MODIFIER_LETTER,OTHER_LETTERNot all letters have case. 并非所有信件都有案例。 Many characters are letters but are neither uppercase nor lowercase nor titlecase. 许多字符都是字母,但既不是大写也不是小写,也不是标题。
The \\p{javaXXX}
character classes is defined in Pattern
API . \\p{javaXXX}
字符类在Pattern
API中定义。
或者,尝试检查它是否包含字母以外的任何内容:[^ A-Za-z]
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表示非字符,那么只需检查其中一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.