我使用preg_replace来突出显示搜索结果中的单词。搜索结果有时也包含URL,而不仅仅是文本。并且一些URL包含关键字。preg_ replace也会更改URL。
有什么方法可以忽略preg_replace中的URL吗?
这就是我使用的:
$result = preg_replace('!('.$keyword.')!i', '<span style="background: #f00;">$1</span>', $result);
谢谢!
已编辑
好吧,比这有用吗
将结果设为数组,然后检查它是否包含url?
<?php
$result = "This is Stpartāāa http://google.lv ";
$arr = explode(" ", $result);
foreach($arr as $key => $value) {
if ((strpos($value,'http://') !== false) AND (strpos($value,'www.') !== false)) {
// do nothing
} else {
// do somthing
}
}
?>