【问题标题】:String contains x amount of numbers and text字符串包含 x 数量的数字和文本
【发布时间】:2015-03-11 05:24:10
【问题描述】:

如果一个字符串包含 4 个数字加上一个空格,然后是文本,我想检查 php。

类似这样的:1234 asdf

If there are more or less then 4 numbers => false  
If there is no text after the numbers => false

我怎样才能以正确的方式做到这一点?

【问题讨论】:

  • php.net/manual/en/function.preg-match.php 执行正则表达式匹配
  • 你能以 any 的方式做到这一点吗?作为一个新手,你应该学习如何实现一些可行的东西,而不是看起来不错的东西。

标签: php string if-statement


【解决方案1】:

试试这个:

$text = '1234 dss';
$returnValue = preg_match('/^[0-9]{4} [a-zA-Z]{1,}$/', $text, $matches);

你可以在这里玩:https://www.functions-online.com/preg_match.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-07
    • 2021-03-04
    • 2017-06-01
    • 1970-01-01
    • 2018-12-18
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多