簡體   English   中英

格式化並在PHP的報告頁面上顯示電話號碼

[英]format and display a phone number on a report page in PHP

我有一個名為customer_phone的數據庫字段,它在輸入時存儲。 它現在從數據庫顯示在報告頁面上,如下所示:

print  "Phone: $customer_phone<br>";

我想從數據庫字段中獲取數據,刪除任何額外的字符(如果有的話只是數字(例如: xxx-xxx-xxxxxxxxxxxxxx ),然后以這種格式顯示在頁面上:

(xxx) xxx-xxxx

我發現函數腳本可以執行此操作但無法使它們工作。

我需要從數據庫字段中獲取數據,重新格式化並在頁面上顯示它。

我是一個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]}";
}

有一個功能要做,就像你問的那樣,稱之為

<?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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM