简体   繁体   English

PHP正则表达式。 检查String是否仅包含字母

[英]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语句,如果有(空格,数字,符号)或除azAZ之外的任何其他内容,将返回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从已经是布尔值返回truefalse是多余的。


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_LETTER

Not 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.

相关问题 PHP 正则表达式检查字符串是否包含大小写字母 - PHP regular expression to check string contains upper and lowercase letters 如何检查字符串在PHP中是否仅包含字母? - How to check if a string contains only letters in PHP? php正则表达式。 仅在第一个单词后面有空格的字母 - php regular expression. Alphabet with spaces only after the first word 使用正则表达式来检查字符串是否以一个下划线和两个字母以php结尾 - Regular Expression to check if string ends with one underscore and two letters with php PHP /正则表达式检查字符串是否包含一定长度的单词 - PHP / regular expression to check if a string contains a word of a certain length 如果php字符串只包含英文字母和数字,如何检查? - How to check, if a php string contains only english letters and digits? mysql中的PHP正则表达式用于检查名称是否包含重音字母且名称长度大于5 - PHP Regular expression in mysql to check for name contains accented letters and length of name greater than 5 检查字符串是否只包含小写字母和下划线 - Check if string contains only lowercase letters and underscores PHP检查字符串是否包含一些字母/数字 - php check if string contains some letters/numbers 如何匹配包含任意字符串的正则表达式,并使用PHP仅将该任意字符串转换为变量? - How to match a regular expression that contains an arbitrary string, and get only that arbitrary string into a variable using PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM