Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-11-16
3
Medium Priority
?
888 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

972 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