Sunday, November 20, 2022

[FIXED] How to delete the first digit number in PHP

Issue

I have a data of phone number starts with +63 and 09

for example the user insert 09123456789 then save. The user shouldn't success if he enters +639123456789 because it was same number at all.

I tried using substr_replace

$data3 = substr_replace("+63", "0",0, 3);

is there other option? i think the substr_replace will have error in the future.

thanks


Solution

If the +63 must be at the start, you may use a preg_replace like

$s = preg_replace('~^\+63~', '0', $s);

Here,

  • ^ - start of a string position
  • \+ - a literal +
  • 63 - a 63 substring.

See the regex demo and a PHP demo.

Please also consider Tpojka's suggestion to use a combination of intl-tel-input on front-end and libphonenumber-for-php on back-end if you need to sanitize and validate international phone numbers.



Answered By - Wiktor Stribiżew
Answer Checked By - Katrina (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.