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.
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
$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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://en.wikipedia.org/wiki/Web_scraping