Link to home
Start Free TrialLog in
Avatar of rajccs
rajccs

asked on

get the any phone number xxx-xxx-xxxx from text

i need to find out any US phone number in this format xxx-xxx-xxxx whether it exist in the text or string using PHP. can anyone help with this.

Avatar of Tyler Laczko
Tyler Laczko
Flag of Canada image

Avatar of Beverley Portlock
Assuming you put your data into a string variable called $data, then the following will pick out matches


$pattern = '~(\d{3}-\d{3}-\d{4})~s';
$matches = array();

if ( preg_match_all( $pattern, $data, $matches, PREG_OFFSET_CAPTURE ) > 0 ) {
    echo "Matches are:<br/>";
    print_r( $matches );
}

The PREG_OFFSET_CAPTURE allows you to determine the offset from the start of the string in characters

ASKER CERTIFIED SOLUTION
Avatar of dsmile
dsmile
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial