【问题标题】:PHP Explode from first number (integer)PHP从第一个数字开始(整数)
【发布时间】:2013-08-03 08:48:36
【问题描述】:

我想展开一些包含第一个数字(整数)的测试。这里有一些话。

Avant Browser 2013 Build 110

Firefox 23.0 Beta 10

Google Chrome 29.0.1547.41 Beta

我正在尝试这个,但它不起作用。

$in ='Avant Browser 2013 Build 110';

preg_match("/\d[^A-Za-z]+([A-Za-z\s]+)/", $in, $match);

echo $match[0];

需要的输出是:-

Avant Browser

Firefox

Google Chrome

请帮忙

【问题讨论】:

    标签: php preg-match explode


    【解决方案1】:

    试试这个正则表达式:

    ^.*?(?=\d)    //start lookup from linestart, get all symbols before first number occurance
    

    【讨论】:

    • 等待 2 分钟以上 .. 作为系统请求 ;)
    • 注意,如果没有数字,这将不起作用。例如:“谷歌浏览器”
    • @invisal 取决于任务和要求。您的解决方案也可以,但匹配空白字符串:)
    • @Sergio,不要误会我的意思。我不是在抱怨你的解决方案。我只是想给 OP 一些额外的信息。从 OP 示例数据中,他可能只想获取浏览器名称,并且有可能存在一些没有版本号的浏览器名称。
    【解决方案2】:

    试试这个正则表达式:

    ^[^0-9]+    // get all non-numeric character and stop when it meets numeric character..
    

    【讨论】:

      【解决方案3】:

      这里使用 preg_match_all

      $txt =<<<EOT
      Avant Browser 2013 Build 110
      Firefox 23.0 Beta 10
      Google Chrome 29.0.1547.41 Beta
      EOT;
      
      preg_match_all('/^([^0-9]*)/m',$txt,$match);
      
      var_dump($match);
      

      【讨论】:

        猜你喜欢
        • 2021-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-09
        • 2011-01-04
        相关资源
        最近更新 更多