简体   繁体   English

将可变格式的电话号码标准化/清理为纯 10 位字符串

[英]Standardize/Sanitize variably-formatted phone numbers to be purely 10-digit strings

Before I store user-supplied phone numbers in my database, I need to standatdize/sanitize the string to consist of exactly 10 digits.在我将用户提供的电话号码存储在我的数据库中之前,我需要对字符串进行标准化/清理,使其恰好包含 10 位数字。

I want to end up with 1112223333 from all of these potential input values:我想从所有这些潜在的输入值中得到1112223333

(111)222-3333
111-222-3333
111.222.3333
+11112223333
11112223333

In the last two strings, there's a 1 as the country code.在最后两个字符串中,有一个 1 作为国家代码。

I was able to make some progress with:我能够在以下方面取得一些进展:

preg_replace('/\D/', '', mysqli_real_escape_string($conn, $_POST["phone"]));

Can anyone help me to fix up the strings that have more than 10 digits?谁能帮我修复超过 10 位数字的字符串?

Using your preg_replace which got all but the last one.使用你的 preg_replace ,除了最后一个。 Next you count the length of the string and remove the first number if it's over 9 numbers.接下来计算字符串的长度,如果超过 9 个数字,则删除第一个数字。

preg_replace('/\D/', '', mysqli_real_escape_string($conn, $_POST["phone"]));

if(strlen($str) > 9){

$str = substr($str, 1);

}

If you want to parse phone numbers, a very useful library is giggsey/libphonenumber-for-php .如果你想解析电话号码,一个非常有用的库是giggsey/libphonenumber-for-php It is based on Google's libphonenumber, it has also a demo online to show how it works它基于谷歌的 libphonenumber,它还有一个在线演示来展示它是如何工作

Do it in two passes:分两次执行:

$phone = [
'(111)222-3333',
'111-222-3333',
'111.222.3333',
'+11112223333',
'11112223333',
'+331234567890',
];

# remove non digit
$res = preg_replace('/\D+/', '', $phone);
# keep only 10 digit
$res = preg_replace('/^\d+(\d{10})$/', '$1', $res);
print_r($res);

Output:输出:

Array
(
    [0] => 1112223333
    [1] => 1112223333
    [2] => 1112223333
    [3] => 1112223333
    [4] => 1112223333
    [5] => 1234567890
)

This task can/should be accomplished by making just one pass over the string to replace unwanted characters.此任务可以/应该通过只遍历字符串来替换不需要的字符来完成。

.*       #greedily match zero or more of any character
(\d{3})  #capture group 1
\D*      #greedily match zero or more non-digits
(\d{3})  #capture group 2
\D*      #greedily match zero or more non-digits
(\d{4})  #capture group 3
$        #match end of string

Matching the position of the end of the string ensures that the final 10 digits from the string are captured and any extra digits at the front of the string are ignored.匹配字符串结尾的位置可确保捕获字符串的最后 10 位数字,并忽略字符串前面的任何额外数字。

Code: ( Demo )代码:(演示

$strings = [
    '(111)222-3333',
    '111-222-3333',
    '111.222.3333',
    '+11112223333',
    '11112223333'
];

foreach ($strings as $string) {
    echo preg_replace(
             '/.*(\d{3})\D*(\d{3})\D*(\d{4})$/',
             '$1$2$3',
             $string
         ) . "\n---\n";
}

Output:输出:

1112223333
---
1112223333
---
1112223333
---
1112223333
---
1112223333
---

The same result can be achieved by changing the third capture group to be a lookahead and only using two backreferences in the replacement string.通过将第三个捕获组更改为前瞻并且仅在替换字符串中使用两个反向引用,可以实现相同的结果。 ( Demo ) 演示

echo preg_replace(
         '/.*(\d{3})\D*(\d{3})\D*(?=\d{4}$)/',
         '$1$2',
         $string
     );

Finally, a much simpler pattern can be used to purge all non-digits, but this alone will not trim the string down to 10 characters.最后,可以使用更简单的模式来清除所有非数字,但仅此一项不会将字符串修剪到 10 个字符。 Calling substr() with a starting offset of -10 will ensure that the last 10 digits are preserved.以 -10 的起始偏移量调用substr()将确保保留最后 10 位数字。 ( Demo ) 演示

echo substr(preg_replace('/\D+/', '', $string), -10);

As a side note, you should use a prepared statement to interact with your database instead of relying on escaping which may have vulnerabilities.作为旁注,您应该使用准备好的语句与您的数据库进行交互,而不是依赖可能存在漏洞的转义。

Use str_replace with an array of the characters you want to remove.将 str_replace 与要删除的字符数组一起使用。

$str = "(111)222-3333 111-222-3333 111.222.3333 +11112223333";

echo str_replace(["(", ")", "-", "+", "."], "", $str);

https://3v4l.org/80AWc https://3v4l.org/80AWc

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

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