简体   繁体   English

使用PHP中的国家/地区代码格式化电话号码

[英]Formatting Phone Numbers with country code in PHP

I am working on website that the customer submit his phone number and need to be able to convert the customer's phone number from 0553322111 to 966553322111 so I can use it to compare with the records in a MySQL database. 我在网站上工作,客户提交他的电话号码,需要能够将客户的电话号码从0553322111966553322111这样我就可以用它来与MySQL数据库中的记录进行比较。

How would I go about this with PHP? 我将如何使用PHP进行此操作?


When the user submit his phone number in the HTML Page 当用户在HTML页面中提交他的电话号码时

He will submitted as 0553322111 . 他将提交为0553322111

PHP PHP

<?php
// store the submitted num.
$temp = $_POST['number']; >> 0553322111.
?> 

I want to use $temp value that stores the number and modify it to 966553322111 so I can query the records with the new value. 我想使用存储数字的$ temp值并将其修改为966553322111以便我可以使用新值查询记录。

Use preg_replace: 使用preg_replace:

$phone_number = preg_replace('/^0/','966',$phone_number);

/^0/ = 0 at the start of the string 字符串开头的/ ^ 0 / = 0

So it basically says, replace 0 at the start of the string with 966 所以它基本上说,在字符串的开头用966替换0

let me know if this works. 让我知道这个是否奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM