I have a huge text file that is a copy and paste job from a PDF phone bill. I need to extract each phone number and put it in an array so that I can insert the numbers into a DB table and run some queries on it.
我有一个很大的文本文件,它是从PDF电话费中复制粘贴过来的。我需要提取每个电话号码,并将其放入一个数组中,这样我就可以将数字插入到数据库表中并对其运行一些查询。
I am using preg_match_all to try and match all of the numbers and failing miserably. Here is the code I have:
我正在使用preg_match_all来尝试匹配所有的数字,并不幸地失败。下面是我的代码:
$phone_list = "13:26 (415)332-5555 13:49 (925)398-5555 13:56 (415)294-3333 14:17 (707)538-2222 14:23 (415)233-1111 14:28 (415)294-0000 14:34";
preg_match_all('/^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/',$phone_list,$matches);
...Which returns nothing. If I shorten the $phone_list to:
…它返回。如果我将$phone_list缩短为:
$phone_list = "(415)294-0000";
...I get a result. What am I missing?
…我得到一个结果。我缺少什么?
Thanks for your help!
谢谢你的帮助!
1 个解决方案
#1
3
Remove the anchors ^
and $
- they force the regex to match the entire string.
删除锚点^和$——他们迫使正则表达式匹配整个字符串。
^
means "Assert that the match starts at the start of the string".
^意味着“断言,比赛从字符串的开始”。
$
means "Assert that the match ends at the end of the string".
$表示“断言匹配在字符串的末尾结束”。
#1
3
Remove the anchors ^
and $
- they force the regex to match the entire string.
删除锚点^和$——他们迫使正则表达式匹配整个字符串。
^
means "Assert that the match starts at the start of the string".
^意味着“断言,比赛从字符串的开始”。
$
means "Assert that the match ends at the end of the string".
$表示“断言匹配在字符串的末尾结束”。