PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

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

 November 20, 2022     laravel, php, preg-replace, regex, regex-negation     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing