小编UC_*_*ITH的帖子

使用 PHP 正则表达式从字符串中提取年份

我想从字符串中提取年份。我得到了一个不完整的解决方案。我的字符串总是这样:

请留言 2014 年和 2015 年以上爵士乐的 mpg 至我的手机号码 +123456789

我尝试过以下正则表达式;

preg_match_all('!\d{4}+| \d{4}+\W!', $str, $matches);
Run Code Online (Sandbox Code Playgroud)

这将输出以下数组。

Array
(
    [0] => Array
        (
            [0] =>  2015 
            [1] =>  2014+
            [2] => 1234
            [3] => 5678
        )

)
Run Code Online (Sandbox Code Playgroud)

我只需要获取带有 + 符号的年份部分(如果有)。即,我只想要这个:

Array
    (
        [0] => Array
            (
                [0] =>  2015 
                [1] =>  2014+                
            )

    )
Run Code Online (Sandbox Code Playgroud)

php regex preg-match-all

2
推荐指数
1
解决办法
2076
查看次数

标签 统计

php ×1

preg-match-all ×1

regex ×1