Jump to content

preg_match help required.


zairyaab

Recommended Posts

Hi,

 

I am fairly new to PHP, I am tryin to put a code together that would see my incoming CLI as 44 and remove the 44 from it and add a 0 to it. I have managed to do this so far, now I am stuck, as any number thats not matching the length (10 digits ) or is not starting with 44 is also not forwarded to my database for query.

 

$input = ($cid);
  if(preg_match('/^(44|0)(\d{10})$/',$input,$matches)){
    $phone = '0'.$matches[2];
 }else
    die("Invalid Phone Number") ;

 

What I would like is that any number starting with 441234567890 to become 01234567890, but should not involve 10 digits criteria.

 

Any help would be appreciated.

 

 

Link to comment
https://forums.phpfreaks.com/topic/289427-preg_match-help-required/
Share on other sites

So you only want to apply it to numbers with more than 10 digits, if it is more replace 44 at the beginning of the number with a zero

 

Simple substr replacement should do the trick.

$num = '441234567890';

// number greater than 10 digits and beings with 44
if(strlen($num) > 10 && substr($num, 0, 2) == 44)
{
    $num = '0' . substr($num, 2); // ignore the first two digits and prepend the number with a zero
}
echo $num;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.