This is a regular expression that will match the 111 (111) 1112 number in the string. You can modify it to work with others.
$string = 'Phone 111 (111) 1112 to get this for 75% off - Call Now. Offer open to 5pm only full price after 5.';
if ( preg_match( '#([0-9]{3}\s*\([0-9]{3}\)\s*[0-9]{4})#',$string,$matches ) === 1 ) {
//use matches array to get phone number
//i think $matches[1] will be the phone number
}
That is an untested regex that I just did.
I am not sure if this is what you wanted, as your questions was kind of confusing and I read through it fast.
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
If you don't mind me asking, why are the phone numbers formatted that way? I have never seen phone numbers like that.
Anyway, you might have run multiple regular expressions.
#([0-9]{3}%[0-9]{3}%[0-9]{4})# //for 111%111%1112
#(\[[0-9]{2}\.[0-9]{1},[0-9]{2}-[0-9]{1}-[0-9]{2}\.[0-9]{2}\])# // for [11.1,11-1-11.12] (i do not think this one will work)
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
Are you wanting to get the individual numbers of each format and create a more readable phone number?
Your last post didn't make much sense to me.
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
Can you post a more complete example of what you are parsing and the code you are doing it with?
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
Why not just use the following???
$str='sd;6sd,af98asd^f76asdf087%45098afsln&k257sdpk235';
$phone=preg_replace('@[^0-9]@','',$str);
echo $phone;
cwarn23
Occupation: Genius
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
ow now I see. I'll work on that regex
cwarn23
Occupation: Genius
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
Try the following:
<?php
$string='Phone 111 (111) 1112 to get this for 75% off - Call Now. Offer open to 5pm only full price after 5.';
preg_match_all('@[^a-zA-Z]{7,}@is',$string,$matches);
$matches=$matches[0];
foreach ($matches AS $key=>$match) {
$matches[$key]=preg_replace('@[^0-9]@s','',$match);
}
echo '<xmp>';
print_r($matches);
echo '</xmp>';
?>
cwarn23
Occupation: Genius
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259