检查字符串是否包含 php 中的字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22458156/
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 04:56:44  来源:igfitidea点击:

check if the string contains alphabets in php

phpregexstringpreg-replacepreg-match

提问by Sabuj Hassan

How to check if the string has alphabets in PHP

如何在PHP中检查字符串是否有字母

The ctype_alphaand ctype_digitdoesn't help in it. is their any method?

而ctype_alphactype_digit不会在它的帮助。他们有什么方法吗?

回答by Sabuj Hassan

You can use preg_matchfor this.

您可以preg_match为此使用。

if(preg_match("/[a-z]/i", $string)){
    print "it has alphabet!";
}

If you want to check for all the characters to be alphabet, then use anchor around the regex. For example ^[a-z]+$.

如果要检查所有字符是否为字母表,请在正则表达式周围使用锚点。例如^[a-z]+$

回答by Sabuj Hassan

Posted as requested

按要求发布

/^[a-zA-Z]+$/alpha, /^[0-9]+$/num, /^[a-zA-Z0-9]+$/alpha-num

/^[a-zA-Z]+$/字母、/^[0-9]+$/数字、/^[a-zA-Z0-9]+$/字母数字

or

或者

/^\pL+$/uUni alpha, /^\pN+$/uUni num, /^[\pL\pN]+$/uUni alpha-num

/^\pL+$/uUni alpha, /^\pN+$/uUni num, /^[\pL\pN]+$/uUni alpha-num