Php preg_将电话号码与删除进行匹配

Php preg_将电话号码与删除进行匹配,php,validation,preg-match,Php,Validation,Preg Match,我试图检查字符串是否包含电话号码。有几种电话号码格式。见下表。我试图将它们添加到preg_匹配检查中,然后可以从字符串中删除它们 06-12341234 - [0-9]{2}[\-][0-9]{8} 0612341234 - [0-9]{10} +31 6 12341234 31612341234 0031 6 12341234 - [0-9]{4}[\s][0-9]{1}[\s][0-9]{8} +31612341234 0031612341234 - [0-9]{11} 06 1234 1

我试图检查字符串是否包含电话号码。有几种电话号码格式。见下表。我试图将它们添加到preg_匹配检查中,然后可以从字符串中删除它们

06-12341234 - [0-9]{2}[\-][0-9]{8}
0612341234 - [0-9]{10}
+31 6 12341234 
31612341234
0031 6 12341234 - [0-9]{4}[\s][0-9]{1}[\s][0-9]{8}
+31612341234
0031612341234 - [0-9]{11}
06 1234 1234 - [0-9]{2}[\s][0-9]{4}[\s][0-9]{4}
06-1234 1234 - [0-9]{2}[\-][0-9]{4}[\s][0-9]{4}
if(preg_match('/
        [0-9]{2}[\-][0-9]{8}| 
        [0-9]{4}[\s][0-9]{1}[\s][0-9]{8}|
        [0-9]{10}|
        [0-9]{11}|
        /', $Message)){
    //URLS
    $pattern = "/
        [0-9]{2}[\-][0-9]{8}| 
        [0-9]{4}[\s][0-9]{1}[\s][0-9]{8}|
        [0-9]{10}|
        [0-9]{11}|
        /";
    $replacement = "[removed]";
    $Message = preg_replace($pattern, $replacement, $string);
    $Score = $Score+20;
}
还有更多。但是有没有更好的办法来查找电话号码并替换它们呢?下面的代码片段不会从字符串中删除电话号码

06-12341234 - [0-9]{2}[\-][0-9]{8}
0612341234 - [0-9]{10}
+31 6 12341234 
31612341234
0031 6 12341234 - [0-9]{4}[\s][0-9]{1}[\s][0-9]{8}
+31612341234
0031612341234 - [0-9]{11}
06 1234 1234 - [0-9]{2}[\s][0-9]{4}[\s][0-9]{4}
06-1234 1234 - [0-9]{2}[\-][0-9]{4}[\s][0-9]{4}
if(preg_match('/
        [0-9]{2}[\-][0-9]{8}| 
        [0-9]{4}[\s][0-9]{1}[\s][0-9]{8}|
        [0-9]{10}|
        [0-9]{11}|
        /', $Message)){
    //URLS
    $pattern = "/
        [0-9]{2}[\-][0-9]{8}| 
        [0-9]{4}[\s][0-9]{1}[\s][0-9]{8}|
        [0-9]{10}|
        [0-9]{11}|
        /";
    $replacement = "[removed]";
    $Message = preg_replace($pattern, $replacement, $string);
    $Score = $Score+20;
}

我认为使用preg_replace()是正确的方法,但您不必首先检查preg_match()。您只需检查字符串是否相同即可。如果不一样,你可以做你需要的其他事情,比如增加分数

因此,您的代码如下所示:

$replacement = "[removed]";
$message = preg_replace('/(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?/', $replacement, $string);
if ($message != $replacement) {
    $score = $score + 20;
}

注意:我找到了上面用于匹配电话号码的模式,我不确定它是否适用于您的数据,您应该自己测试它。

尝试将字母
x
添加到您的模式末尾:

与此相反:

$pattern = '/
        [0-9]{2}[\-][0-9]{8}| 
        [0-9]{4}[\s][0-9]{1}[\s][0-9]{8}|
        [0-9]{10}|
        [0-9]{11}|
        /';
使用以下命令:

$pattern = '/
        [0-9]{2}[\-][0-9]{8}| 
        [0-9]{4}[\s][0-9]{1}[\s][0-9]{8}|
        [0-9]{10}|
        [0-9]{11}|
        /x';
更好的方法是,定义一次模式并重用它:

// If the pattern is the same, there's no point in writing it twice.
// Save some time and possibly prevent errors by using the same pattern
// in each check.
$pattern = '/
        [0-9]{2}[\-][0-9]{8}| 
        [0-9]{4}[\s][0-9]{1}[\s][0-9]{8}|
        [0-9]{10}|
        [0-9]{11}|
        /x';
if (preg_match($pattern, $Message)) {
    //URLS
    $replacement = "[removed]";
    $Message = preg_replace($pattern, $replacement, $string);
    $Score = $Score+20;
}

嗨,刚刚测试了你的更改。但是,它会删除整个字符串,而不是其中的一部分。我应该更改什么以只删除电话号码?我看不到任何地方定义了
$string
,所以我不确定这是从哪里来的。。。您是否试图从
preg_match()
中生成匹配数组?或者你想换哪一部分?