[英]Remove leading 0 from phone number in Woocommerce checkout field
I am trying to remove the leading '0' from the phone number so that if a customer enters his/her phone number as '0856xxxxxxx' it turns to '856xxxxxxx' removing leading zero. 我正在尝试从电话号码中删除开头的“ 0”,以便如果客户输入自己的电话号码为“ 0856xxxxxxx”,则变成“ 856xxxxxxx”,删除开头的零。 I did some research on Internet and found that ltrim() function can be used so I updated my code in functions.php as below:
我在Internet上进行了一些研究,发现可以使用ltrim()函数,因此我按如下所示更新了functions.php中的代码:
add_action('woocommerce_checkout_process', 'removeLeadingZero');
function removeLeadingZero() {
$billing_phone = filter_input(INPUT_POST, 'billing_phone');
$billing_phone= ltrim($billing_phone, '0');
}
Above code is not giving any output. 上面的代码没有给出任何输出。 Now the problem is I don't know how to return the updated value so that the final value becomes as desired.
现在的问题是我不知道如何返回更新后的值,以使最终值变为所需的值。
Thanks 谢谢
My given code is able to remove the leading zero, the problem I am facing is to update the number in the checkout field. 我给定的代码能够删除前导零,我面临的问题是更新结帐字段中的数字。
$num = "0852360852";
if(stripos($num,"0") === 0){
echo ltrim($num,"0");
}
did you try something like : 您是否尝试过类似的方法:
<?php
add_action('woocommerce_checkout_process', 'removeLeadingZero');
function removeLeadingZero() {
if (isset($_POST['billing_phone'])) {
$_POST['billing_phone'] = preg_replace('/^0/', '', $_POST['billing_phone']);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.