I want to check user input in the textbox. If the first character is a zero, it will be replaced with +63
. But it should not replace all the zeroes in the string. I had search through sites but most are about replacing all of the occurrences. Thanks a lot!
In PHP:
<?php
$ptn = "/^0/"; // Regex
$str = "01234"; //Your input, perhaps $_POST['textbox'] or whatever
$rpltxt = "+63"; // Replacement string
echo preg_replace($ptn, $rpltxt, $str);
?>
Would echo out:
+631234
Of course, you'll want to validate the input and everything, but that's how you'd replace it after it's been submitted.
JavaScript solution
var str = "000123";
var foo = str.replace(/^0/,"+63");
alert(foo);
Basic regular expression
PHP Solution
$string = '000123';
$pattern = '/^0/';
$replacement = '+63';
echo preg_replace($pattern, $replacement, $string);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With