我得到了以下函数,其中包含一个正则表达式
public function searchDOM( $content, $search, $replace, $excludedParents = [] )
{
$dom = HtmlDomParser::str_get_html(
$content,
true,
true,
DEFAULT_TARGET_CHARSET,
false,
DEFAULT_BR_TEXT,
DEFAULT_SPAN_TEXT
);
foreach ( $dom->find( 'text' ) as $element ) {
if ( !in_array( $element->parent()->tag, $excludedParents ) )
$element->innertext = preg_replace(
'/\b' . preg_quote( $search, "/" ) . '\b/i',
$replace,
$element->innertext
);
}
我将$search
传递给函数,如下所示:
foreach ( $searcharray as $search ) {
$text = $search[ 'text' ];
$url = $search[ 'url' ];
$replace = "<a href='$url'>\$0</a>";
$content = $this->searchDOM(
$content,
$text,
$replace,
[ 'a', 'img', 'script', 'style', 'code', 'pre' ]
);
}
工作正常。但是,如果搜索字符串包含+
,例如检测失败。
我该如何解决?
答案 0 :(得分:1)
由于您的搜索短语可能包含非单词字符,因此您应该考虑将它们从最长的字符开始按降序传递给您的函数。首先,传递hello world
,然后传递hello
,然后传递world
等等。
现在,如果您在搜索词组的开头或结尾有一个非单词字符,/\b\+search\b/i
在+search
中不会与I want +search.
匹配,因为没有单词空格和+
之间的边界(因为两者都是非单词字符)。但是,它会在I want+search
中找到匹配项,因为t
和+
之间存在单词边界。
使用lookarounds来断言前面/后面跟着单词char的位置:
'/(?<!\w)' . preg_quote( $search, "/" ) . '(?!\w)/i'
但是可能会出现另一个问题:您已经更换的内容可以再次更换。为避免这种情况,请从text
项构建动态正则表达式,使用texts-url准备一个数组,然后使用preg_replace_callback
这是一个示例演示,根据您的输入,查找代码可能会有所不同(我没有将其包装到一个函数中,您可以轻松地在最后执行此操作):
$searches= array();
$searches = array(
array("text" => "hello", "url" => "hello-URL"),
array("text" => "world", "url" => "world-URL"),
array("text" => "hello world", "url" => "helloworld-URL")
);
usort($searches, function($a, $b) {
return strlen($b['text']) - strlen($a['text']);
});
$pat = '~(?<!\w)(?:' . implode("|",
array_map(function($x) { return preg_quote($x['text'], '~'); }, $searches)
) . ')(?!\w)~i';
// echo "$pat\n"; // => ~(?<!\w)(?:hello world|hello|world)(?!\w)~i
$keys = array();
$vals = array();
foreach($searches as $search) {
$keys[] = $search['text'];
$vals[] = $search['url'];
}
$tmparr = array_combine($keys, $vals);
$text = preg_replace_callback($pat, function($m) use ($tmparr) {
return "<a href='" . $tmparr[$m[0]] . "'>" . $m[0] . "</a>"; }, $text);
echo $text;
请参阅PHP demo。