用PHP在报告页面上格式化并显示电话号码
我有一个名为用PHP在报告页面上格式化并显示电话号码,php,phone-number,Php,Phone Number,我有一个名为customer\u phone的数据库字段,它在输入时存储。它现在显示在数据库的报告页面上,如下所示: print "Phone: $customer_phone<br>"; 我已经找到了函数脚本来实现这一点,但还不能让它们工作 我需要从数据库字段获取数据,重新格式化并在页面上显示 我是一个PHP新手,非常感谢您的帮助 $phone_number= preg_replace('/[^0-9]+/', '', $phone_number); //Strip all n
customer\u phone
的数据库字段,它在输入时存储。它现在显示在数据库的报告页面上,如下所示:
print "Phone: $customer_phone<br>";
我已经找到了函数脚本来实现这一点,但还不能让它们工作
我需要从数据库字段获取数据,重新格式化并在页面上显示
我是一个PHP新手,非常感谢您的帮助
$phone_number= preg_replace('/[^0-9]+/', '', $phone_number); //Strip all non number characters
echo preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone_number) //Re Format it
有一个函数可以按你的要求执行
<?php echo format_telephone($customer_phone); ?>
没问题,如果您能决定胜利者并接受答复,我们将不胜感激:)
function format_telephone($phone_number)
{
$cleaned = preg_replace('/[^[:digit:]]/', '', $phone_number);
preg_match('/(\d{3})(\d{3})(\d{4})/', $cleaned, $matches);
return "({$matches[1]}) {$matches[2]}-{$matches[3]}";
}
<?php echo format_telephone($customer_phone); ?>