Solved

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

Posted on 2010-11-16
3
853 Views
Last Modified: 2012-05-10
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.

0
Comment
Question by:rajccs
3 Comments
 
LVL 10

Expert Comment

by:Tyler Laczko
ID: 34146077
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34146159
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

0
 
LVL 13

Accepted Solution

by:
dsmile earned 500 total points
ID: 34152437
If you intend to "grab" US phone number, then take a look at this article http://www.macronimous.com/resources/writing_regular_expression_with_php.asp

It provides detailed explanation on each pattern that match US phone number format and also an pratical example to grab phone number on a website.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question