Php Regex识别电话号码
我有一个要求,我必须在用户提供的信息中隐藏电话号码。我已经有一个正则表达式,如下所示:Php Regex识别电话号码,php,regex,drupal-7,Php,Regex,Drupal 7,我有一个要求,我必须在用户提供的信息中隐藏电话号码。我已经有一个正则表达式,如下所示: /\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/ This 1 is the very good case to solve. 但这只能识别以下格式的手机号码: 9876543210 我希望它也涵盖以下格式: 987654210 9876542310 (987)6543210 (987)(654)(3210) 在上述所有格式中,空格可以替换为“-”或“
/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/
This 1 is the very good case to solve.
但这只能识别以下格式的手机号码:
9876543210
我希望它也涵盖以下格式:
987654210
9876542310
(987)6543210
(987)(654)(3210)
在上述所有格式中,空格可以替换为“-”或“.”。此外,“(”和“')”可以替换为“[”和“]”
此外,是否可以识别用字符串而不是数字提及的电话号码,如
九八七六五四三二一零
数字和字符串的任意组合
编辑:添加我的功能,该功能在内容中隐藏联系人号码(如果有的话):
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');
(您可以不断添加数字以支持,如10、11等)
您还可以使用regexp来匹配数字和字符串的组合。例如,修改我使用的一个:
\(?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])-[0-9])([0-9])([0-9])([0-9])([0-9])-]
将匹配您的所有示例。
此处演示:
要获得完整的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以设置它将替换匹配项的内容
这里有完整的演示:为寻找类似解决方案的任何人发布此内容。在上面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;
}
仅供参考:这只有一个问题,即如果文本格式中提到了一个数字,它将转换为实际数字。对于e、 g.如果有以下行:
This one is the very good case to solve.
上述行将按如下方式进行转换:
/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/
This 1 is the very good case to solve.
首先,您应该尝试进一步了解您的regexp正在做什么。它不仅匹配9876543210
,还匹配:9876543210
或987-654-3210
或987.654.3210
。请查看该网站,它将帮助您更好地了解它的工作原理。是的,我了解这一点,但实际上,当我尝试运行“987 654 3210”或“987-654-3210”或“987.654.3210”时,它不起作用。然后,您必须提供更多关于不起作用的代码,因为正如您在这里看到的:您的regexp与上述3种情况相匹配。添加了识别数字并隐藏数字的my函数。请检查。你让我的眼睛流血:)请看我更新的答案,preg\u replace
足以替换匹配项。我尝试了你的解决方案,尽管它在演示中有效,但当我将它包含在函数中时,它并不能识别提到的所有情况。请在编辑中检查我的功能