识别 phone 数字的正则表达式
Regex to identify phone number
我有一个要求,我必须在用户提供的消息中隐藏 phone 号码。我已经有一个正则表达式如下:
/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/
但只能识别以下格式的手机号码:
9876543210
我也希望它涵盖以下格式:
987 654 3210
9 8 7 6 5 4 3 2 1 0
(987) 654 3210
(987) (654) (3210)
在上述所有格式中,空格都可以替换为“-”或“.”。此外,'(' 和 ')' 可以替换为 '[' 和 ']'。
此外,是否可以识别 phone 用字符串而不是数字提及的数字,例如
Nine eight seven six five four three two one zero
Any combination of digits and strings
编辑: 添加我的功能,该功能隐藏内容中的联系电话号码:
function hide_contact_number($description) {
// Find contact number and hide it!
$regex = "/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/";
/*$regex = "/[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{4})[\)\]]?|([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])/";*/
if(preg_match_all($regex, $description, $matches, PREG_OFFSET_CAPTURE)) {
foreach($matches as $matchkey => $match) {
foreach($match as $key => $value) {
$index = 0;
$length = 0;
if(is_array($value)) {
if(is_numeric($value[0]) && strlen($value[0]) >= 10) {
$index = $value[1];
$length = strlen($value[0]);
} else if(strlen($value[1]) >= 10) {
$index = $value[0];
$length = strlen($value[1]);
} else {
// TODO: Do nothing
}
}
if($length > 0) {
// length - 2 => 2 places before end of email id including 1 of index + 1
$description = substr_replace($description, str_repeat("*", $length-2), $index+1, $length-2);
}
}
}
}
return $description;
}
上述函数并没有识别和隐藏我提到的所有数字序列。即使@CCH 的解决方案也无济于事。这个函数有什么问题吗?
所有这些情况的一个快速简单的解决方案是创建一个只有数字的时间变量。
我不知道 PHP,但在 JS 中(你当然可以适应它)它将是:
aux = string.replace(/\D/g, '')
然后将您的正则表达式应用于 aux 变量。
匹配所有情况的正则表达式会很丑陋,但我开始了:
\(?\d\s*\d\s*\d\)\s*\(?\d\s*\d\s*\d\)\s*\(?\d\s*\d\s*\d\s*\d)
还有东西这个词,你可以随时做:
number = string
.replace(/one/g, '1')
.replace(/two/g, '2')
.replace(/three/g, '3')
.replace(/four/g, '4')
.replace(/five/g, '5')
.replace(/six/g, '6')
.replace(/seven/g, '7')
.replace(/eight/g, '8')
.replace(/nine/g, '9')
.replace(/zero/g, '0');
(可以继续加数字支持,比如十、十一等。)
您也可以使用正则表达式来匹配数字和字符串的组合。比如修改我用的那个:
\(?d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\)?\s*\(?d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\)?\s*\(?d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\)?
(我真的不建议这样做)
这个:
[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{4})[\)\]]?|([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])
将匹配您的所有示例。
此处演示:
https://regex101.com/r/h9631Z/4
要获得完整的 php 功能,请使用:
function hide_contact_number($description) {
$re = '/[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{4})[\)\]]?|([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])/';
$subst = '*** *** ***';
return preg_replace($re, $subst, $description);
}
您可以更改 $subst 以设置它将替换匹配项的内容。
完整演示在这里:https://repl.it/FnSp/3
将此发布给正在寻找类似解决方案的任何人。在上面 CCH 的回答(已接受)和 dquijada 的帮助下,我想出了以下功能来隐藏内容中的联系电话。
function hide_contact_number($description) {
$search = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
$replace = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
$description = str_ireplace($search, $replace, $description);
$regex = '/[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{4})[\)\]]?' .
'|([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*/';
$description = preg_replace($regex, str_repeat('*', 10), $description);
return $description;
}
仅供参考: 这只有一个问题,即,如果文本格式中提到的数字,它将被转换为实际数字。为了。例如如果有下面一行:
This one is the very good case to solve.
以上行将按如下方式转换:
This 1 is the very good case to solve.
我有一个要求,我必须在用户提供的消息中隐藏 phone 号码。我已经有一个正则表达式如下:
/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/
但只能识别以下格式的手机号码:
9876543210
我也希望它涵盖以下格式:
987 654 3210
9 8 7 6 5 4 3 2 1 0
(987) 654 3210
(987) (654) (3210)
在上述所有格式中,空格都可以替换为“-”或“.”。此外,'(' 和 ')' 可以替换为 '[' 和 ']'。
此外,是否可以识别 phone 用字符串而不是数字提及的数字,例如
Nine eight seven six five four three two one zero
Any combination of digits and strings
编辑: 添加我的功能,该功能隐藏内容中的联系电话号码:
function hide_contact_number($description) {
// Find contact number and hide it!
$regex = "/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/";
/*$regex = "/[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{4})[\)\]]?|([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])/";*/
if(preg_match_all($regex, $description, $matches, PREG_OFFSET_CAPTURE)) {
foreach($matches as $matchkey => $match) {
foreach($match as $key => $value) {
$index = 0;
$length = 0;
if(is_array($value)) {
if(is_numeric($value[0]) && strlen($value[0]) >= 10) {
$index = $value[1];
$length = strlen($value[0]);
} else if(strlen($value[1]) >= 10) {
$index = $value[0];
$length = strlen($value[1]);
} else {
// TODO: Do nothing
}
}
if($length > 0) {
// length - 2 => 2 places before end of email id including 1 of index + 1
$description = substr_replace($description, str_repeat("*", $length-2), $index+1, $length-2);
}
}
}
}
return $description;
}
上述函数并没有识别和隐藏我提到的所有数字序列。即使@CCH 的解决方案也无济于事。这个函数有什么问题吗?
所有这些情况的一个快速简单的解决方案是创建一个只有数字的时间变量。
我不知道 PHP,但在 JS 中(你当然可以适应它)它将是:
aux = string.replace(/\D/g, '')
然后将您的正则表达式应用于 aux 变量。
匹配所有情况的正则表达式会很丑陋,但我开始了:
\(?\d\s*\d\s*\d\)\s*\(?\d\s*\d\s*\d\)\s*\(?\d\s*\d\s*\d\s*\d)
还有东西这个词,你可以随时做:
number = string
.replace(/one/g, '1')
.replace(/two/g, '2')
.replace(/three/g, '3')
.replace(/four/g, '4')
.replace(/five/g, '5')
.replace(/six/g, '6')
.replace(/seven/g, '7')
.replace(/eight/g, '8')
.replace(/nine/g, '9')
.replace(/zero/g, '0');
(可以继续加数字支持,比如十、十一等。) 您也可以使用正则表达式来匹配数字和字符串的组合。比如修改我用的那个:
\(?d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\)?\s*\(?d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\)?\s*\(?d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\)?
(我真的不建议这样做)
这个:
[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{4})[\)\]]?|([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])
将匹配您的所有示例。
此处演示:
https://regex101.com/r/h9631Z/4
要获得完整的 php 功能,请使用:
function hide_contact_number($description) {
$re = '/[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{4})[\)\]]?|([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])/';
$subst = '*** *** ***';
return preg_replace($re, $subst, $description);
}
您可以更改 $subst 以设置它将替换匹配项的内容。
完整演示在这里:https://repl.it/FnSp/3
将此发布给正在寻找类似解决方案的任何人。在上面 CCH 的回答(已接受)和 dquijada 的帮助下,我想出了以下功能来隐藏内容中的联系电话。
function hide_contact_number($description) {
$search = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
$replace = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
$description = str_ireplace($search, $replace, $description);
$regex = '/[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{4})[\)\]]?' .
'|([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*/';
$description = preg_replace($regex, str_repeat('*', 10), $description);
return $description;
}
仅供参考: 这只有一个问题,即,如果文本格式中提到的数字,它将被转换为实际数字。为了。例如如果有下面一行:
This one is the very good case to solve.
以上行将按如下方式转换:
This 1 is the very good case to solve.