[英]format and display a phone number on a report page in PHP
I have a database field called customer_phone
that is stored as it was inputted. 我有一个名为
customer_phone
的数据库字段,它在输入时存储。 It is displayed on a report page now from the database like this: 它现在从数据库显示在报告页面上,如下所示:
print "Phone: $customer_phone<br>";
I'd like to get the data from the database field, strip out any extra characters if any to just numbers (ex: xxx-xxx-xxxx
to xxxxxxxxxx
) and then display it on the page in this format: 我想从数据库字段中获取数据,删除任何额外的字符(如果有的话只是数字(例如:
xxx-xxx-xxxx
到xxxxxxxxxx
),然后以这种格式显示在页面上:
(xxx) xxx-xxxx
I've found function scripts to do this but haven't been able to get them to work. 我发现函数脚本可以执行此操作但无法使它们工作。
I need to get the data from the database field, re-format it and display it on the page. 我需要从数据库字段中获取数据,重新格式化并在页面上显示它。
I'm a PHP newby and would appreciate any help. 我是一个PHP新手,非常感谢任何帮助。
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]}";
}
There's a function to do as you asked, call it like 有一个功能要做,就像你问的那样,称之为
<?php echo format_telephone($customer_phone); ?>
$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.