[英]Trim the first digit of a Phone number to be in International format
Am working on a web application whereby am capturing phone number from the user, but on the backend (build in Laravel PHP) I want to trim the first digit from the phone number and replace it with 254 . 我正在开发一个Web应用程序,从而从用户那里捕获电话号码,但是在后端(在Laravel PHP中构建)上,我想修剪电话号码的第一位并将其替换为254 。
For instance,, am capturing this phone number 07******23**
例如,正在捕获此电话号码
07******23**
I need to replace the first zero with 254
so that it can be 2547******23**
我需要将第一个零替换为
254
以便可以是2547******23**
You don't even need to invoke regex here, substr()
should work just fine: 您甚至不需要在这里调用正则表达式,
substr()
应该可以正常工作:
$input = "07123456789";
$output = "254" . substr($input, 1);
If you only want to do this replacement on numbers beginning with zero, then it might make more sense to use preg_replace
: 如果只想对从零开始的数字进行此替换,那么使用
preg_replace
可能更有意义:
$output = preg_replace("/^0/", "254", $input);
Use ltrim to remove the zero. 使用ltrim删除零。
It will remove the zero if it's there, and leave the string intact if not. 如果存在零,它将删除零,如果不存在,则将字符串保留完整。
echo "254" . ltrim($number, "0");
See example with and without the leading zero: 请参见有无前导零的示例:
https://3v4l.org/Z03st https://3v4l.org/Z03st
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.