Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP find phone number in a string with all possible combinations

Tags:

php

I got it working when the number is separated with -. How can I add the parameters for when the numbers are like this:

(xxx) xxx xxxx
xxx.xxx.xxxx
xxx-xxx-xxxx
(xxx)xxx-xxxx
xxxxxxxxxx
xxx xxx xxxx

the php code:

//find phone in string
preg_match('/\b\d{3}\s*-\s*\d{3}\s*-\s*\d{4}\b/', $phone, $phone_matches); 
$find_phone = $phone_matches[0];

EDIT: I found a way to replace it within the text. So I am still looking for all the formats to apply

$hide_phone = preg_replace('/\b\d{3}\s*-\s*\d{3}\s*-\s*\d{4}\b/', '[hidden phone]', $phone);
like image 753
BragDeal Avatar asked Oct 31 '25 21:10

BragDeal


1 Answers

different approeach:

"/[(]*\d{3}[)]*\s*[.\-\s]*\d{3}[.\-\s]*\d{4}/"
like image 192
Desolator Avatar answered Nov 02 '25 12:11

Desolator