【问题标题】:check if the string contains alphabets in php检查字符串是否在php中包含字母
【发布时间】:2014-04-22 21:10:03
【问题描述】:

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

ctype_alphactype_digit 无济于事。他们有什么方法吗?

【问题讨论】:

  • 可以发ctype_alpha的失败测试用例吗?
  • @ShankarDamodaran 它只检查字母.. 如果它找到 alhanumeric 它返回 false
  • ctype_alpha 如果文本中的每个字符都是来自当前语言环境的字母,则返回TRUE,否则返回FALSE。您的字符串是否包含所有字母?
  • /^[a-zA-Z]+$/ alpha,/^[0-9]+$/ num,/^[a-zA-Z0-9]+$/ alpha-num。
  • 或者,/^\pL+$/ Uni alpha,/^\pN+$/ Uni num,/^[\pL\pN]+$/ Uni alpha-num

标签: php regex string preg-replace preg-match


【解决方案1】:

您可以为此使用preg_match

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

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

【讨论】:

    【解决方案2】:

    按要求发布

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

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

    【讨论】:

      猜你喜欢
      • 2015-01-04
      • 2012-05-01
      • 2011-07-11
      • 1970-01-01
      • 2020-12-12
      • 2014-07-16
      • 2011-05-08
      • 1970-01-01
      相关资源
      最近更新 更多