Solved

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

Posted on 2010-11-16
3
843 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
Comment Utility
0
 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

763 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

8 Experts available now in Live!

Get 1:1 Help Now