【发布时间】:2014-01-02 13:38:29
【问题描述】:
在我的一个 PHP 网站上,我使用 this regular expression 自动从字符串中删除电话号码:
$text = preg_replace('/\+?[0-9][0-9()-\s+]{4,20}[0-9]/', '[removed]', $text);
但是,当用户发布包含多个数字作为其文本一部分的长 URL 时,该 URL 也会受到 preg_replace 的影响,从而破坏 URL。
如何确保上述preg_replace 不会更改$text 中包含的URL?
编辑:
根据要求,这是一个 URL 被上面的 preg_replace 破坏的示例:
$text = 'Please help me with my question here: https://stackoverflow.com/questions/20589314/ Thanks!';
$text = preg_replace('/\+?[0-9][0-9()-\s+]{4,20}[0-9]/', '[removed]', $text);
echo $text;
//echoes: Please help me with my question here: https://stackoverflow.com/questions/[removed]/ Thanks!
【问题讨论】:
-
只需检查违规文本是否以“http”开头。
-
@nietonfir:但是如果 URL 在文本中间呢?
-
我认为您必须解析网址和电话号码,例如
/(?: url \K | phone number)/ -
请提供几个带有电话号码的 URL 示例,以及它们是如何被破坏的
-
@sln:我该怎么做?如果有帮助,这里有一个 URL 正则表达式:stackoverflow.com/a/8234912/869849
标签: php regex string url preg-replace