Jump to content


Photo

preg_match help required.

preg_match php

Best Answer Ch0cu3r, 04 July 2014 - 04:47 AM

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;
Go to the full post


  • Please log in to reply
2 replies to this topic

#1 zairyaab

zairyaab

    Newbie

  • New Members
  • Pip
  • 4 posts

Posted 03 July 2014 - 03:52 PM

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.

 

 



#2 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,100 posts

Posted 04 July 2014 - 04:47 AM   Best Answer

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;


#3 zairyaab

zairyaab

    Newbie

  • New Members
  • Pip
  • 4 posts

Posted 04 July 2014 - 06:14 AM

Thanks a lot @Ch0cu3r you saviour! That code is exactlly what I was lookin for! You genius, saved me!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com