0

我正在使用 preg_replace 突出显示搜索结果中的单词。搜索结果有时还包含 URL,而不仅仅是文本。一些 URL 包含关键词。然后由于 preg_replace 也会更改 URL,因此 URL 会变得混乱。

有什么方法可以忽略 preg_replace 中的 URL?

这就是我使用的:

$result = preg_replace('!('.$keyword.')!i', '<span style="background: #f00;">$1</span>', $result);

谢谢你!

4

1 回答 1

1

编辑..
好吧,这有帮助吗?
将结果设为数组,然后检查它是否包含 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
            }
    }
    ?>
于 2012-08-23T11:20:20.177 回答