用PHP格式化电话号码 [英] Formatting Phone Numbers in PHP

查看:67
本文介绍了用PHP格式化电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SMS应用,并且需要能够将发件人的电话号码从 +11234567890 转换为 123-456-7890 ,因此可以将其与 MySQL数据库中的记录进行比较.

I am working on an SMS app and need to be able to convert the sender's phone number from +11234567890 to 123-456-7890 so it can be compared to records in a MySQL database.

数字以后一种格式存储,可在网站上的其他地方使用,我宁愿不更改该格式,因为它将需要修改很多代码.

The numbers are stored in the latter format for use elsewhere on the site and I would rather not change that format as it would require modifying a lot of code.

我将如何使用PHP?

谢谢!

推荐答案

$data = '+11234567890';

if(  preg_match( '/^\+\d(\d{3})(\d{3})(\d{4})$/', $data,  $matches ) )
{
    $result = $matches[1] . '-' .$matches[2] . '-' . $matches[3];
    return $result;
}

这篇关于用PHP格式化电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆