2

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 numbers 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!

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Tim...this was exactly what I need. Sometimes RegEx feels like Sanskrit to me. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.