Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 替换文本中的单词忽略URL_Php_Regex_Replace_Preg Replace - Fatal编程技术网

Php 替换文本中的单词忽略URL

Php 替换文本中的单词忽略URL,php,regex,replace,preg-replace,Php,Regex,Replace,Preg Replace,我使用这一行搜索带有url的文本中的名称 $name = "xxx"; $text = "my name is xxx en my website is http://xxx.something.com"; $text = preg_replace("/\b(".preg_quote($name, "/").")\b/i", $url, $text); 如何更改此正则表达式以忽略文本中的URL(1)您可以使用正则表达式查找所有URL(例如),记住它们的位置和长度 2) 使用正则表达式查找单词的

我使用这一行搜索带有url的文本中的名称

$name = "xxx";
$text = "my name is xxx en my website is http://xxx.something.com";
$text = preg_replace("/\b(".preg_quote($name, "/").")\b/i", $url, $text);
如何更改此正则表达式以忽略文本中的URL(1)您可以使用正则表达式查找所有URL(例如),记住它们的位置和长度

2) 使用正则表达式查找单词的所有出现处,并记住位置和长度


3) 查看2)的结果,并检查该单词是否不在url中。

不完全是您想要的,但可能有以下帮助:

当且仅当
xxx
被空格包围或位于字符串的开头或结尾时,将
xxx
替换为
yyy

<?
$name = "xxx";
$text = "xxx my name is xxx en my website xxx is http://xxx.something.com xxx";
$text = preg_replace("%(?<=^| )".$name."(?= |$)%i", "yyy", $text);
echo $text."\n";
//yyy my name is yyy en my website yyy is http://xxx.something.com yyy
?>


如果PHP支持可变长度查找断言的后面,那么就更容易了;因为那时我们可以使用更精确的
(?

我对您建议的更精确的解决方案非常感兴趣。我认为PHP现在支持所有必要的功能。我已经尝试了
/(?但这只匹配整个单词,用空格分隔,但不匹配
yxxy
xxxy
。任何提示都将不胜感激。https支持也将非常方便。我无法理解这里的逻辑。