user5405873 user5405873 - 3 months ago 10
PHP Question

how to extract phone/mobile number from a big string using regex in php

I have a big string which looks something like shown below.
i want to extract those 3 numbers which are in the string
1.9886991201 2.+7189674123 3.(080)-2235456

$var = '

rst post. Edit or delete it, then start writing!

will change the whole world

Search for:
Search …
RECENT POSTS

This is my first blog post
Hello world!
RECENT COMMENTS

Mr WordPress on Hello world!
ARCHIVES

August 2016
July 2016
CATEGORIES

Uncategorized
META

Log in
Entries RSS
Comments RSS
WordPress.org
Proudly powered by WordPress


dajsnjdgffffffffffffffffffffffffffffffffffz
9886991201...dfsdsddfgf
ddffgbnfhgjhjgjg
dfknfkjkndfdjkfdjjjjjjjjjjjjjjjj
mskjndkjdnndnjdfjjdm
njcddfdnfnzdfdzfdzfdj+7189674123msdfjgfdxfds
dwasnkbnznfvnndfv
aefnbhjmbffdsnfnndv
dmlnjdnvdnsnvdndv
dLKMNJFNNFVNFGN
DNBFDBFJFDJF
do code...
while (fsdkmnbv vx b s
fgzd zvn bmbfmbfxmbfxm);,dwdfen (080)-2235456 ananczzzzzzzzzz bcxxx AS SNDMCKJASOL P0-`0-1`1WE2IQ82EYQWASHBZXNdwhbjbjdhhhhhhhhhhhhhhhhhas
dmfndsvfncx

';

Answer

As per your description, I see three different patterns of number that you want to search.

  1. 10 digits
  2. + sign followed by 10 digits
  3. (xxx)-xxxxxxX format

This patterns can be matched using the following regex

/[\+]?\d{10}|\(\d{3}\)\s?-\d{6}/

https://regex101.com/r/dJ9cV2/5

https://regex101.com/r/dJ9cV2/6

https://regex101.com/r/dJ9cV2/7