1

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

4
  • try str_replace('+63','0')
    – PHP Ninja
    May 23, 2019 at 7:29
  • substr_replace is probably a safer option than str_replace
    – Andreas
    May 23, 2019 at 7:33
  • I don't see the problem you are trying to solve. As I see it this question is off topic on SO. It could probably be posted on code review though
    – Andreas
    May 23, 2019 at 7:35
  • Those things are already solved. I would suggest using intl-tel-input on frontend and libphonenumber-for-php on backend.
    – Tpojka
    May 23, 2019 at 9:14

2 Answers 2

3

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.

0
1

In this case you can have a condition to check and have str_replace

if there is a + on your request then make remove the first 3 letters including +

if (substr($phone, 0, 1) == '+') {
    $phone = str_replace(substr($phone, 0, 3), '0', $phone);
}

$phone_number = $phone;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.