开发者

Using PHP to Extract Multiple Phone numbers from a string or file

开发者 https://www.devze.com 2023-02-14 20:00 出处:网络
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 quer

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.

I am using preg_match_all to try and match all of the numb开发者_如何学Goers and failing miserably. Here is the code I have:

$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 = "(415)294-0000";

...I get a result. What am I missing?

Thanks for your help!


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".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号