Solved

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

Posted on 2010-11-16
3
848 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now