我正在尝试从电话号码中删除前导'0',以便如果客户输入他/她的电话号码为'0856xxxxxxx',则转为'856xxxxxxx',删除前导零。我在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');
}
上面的代码没有给出任何输出。现在问题是我不知道如何返回更新的值,以便最终值变为所需。
由于
答案 0 :(得分:0)
我的代码能够删除前导零,我面临的问题是更新结帐字段中的数字。
$num = "0852360852";
if(stripos($num,"0") === 0){
echo ltrim($num,"0");
}
答案 1 :(得分:0)
<?php
add_action('woocommerce_checkout_process', 'removeLeadingZero');
function removeLeadingZero() {
if (isset($_POST['billing_phone'])) {
$_POST['billing_phone'] = preg_replace('/^0/', '', $_POST['billing_phone']);
}
}