Someone in Stack Overflow gave me this code:
$output = preg_replace( '/(\+?\d{2}|0)(\d{7,8})/', '0$2', $test);
It changes user input:
+622112345 into 02112345
622112345 into 02112345
02112345 into 02112345
Unfortunately it only works for 7-8 digits. I decided to change the code into this:
$output = preg_replace( '/(\+?\d{2}|0)(\d{15})/', '0$2', $test);
By changing (\d{7,8})
into (\d{15})
, I hoped I could get this RegEx to validate up to 15 digits. But, here's what I have:
input : 083812345678910 >> output : 083812345678910 [correct]
input : 6283812345678910 >> output : 6283812345678910 [false, should be : 083812345678910]
input : +6283812345678910 >> output : 6283812345678910 [false, should be : 083812345678910]
How can I make this works on 15 digit numbers? Thanks.
UPDATE : user input can be 10, 11, 12 or even 13 digits. but not more than 15 digits. so I need this code to change the prefix : either +62, 62 or 0 INTO 0xxxx. no matter how many digits they have, 15 maximum.