可以参考这个,不知道这个是不是你做的
function search_keyword_highlight($s, $keyword_arr) {
foreach($keyword_arr as $key => $keyword) {
if($key >= 1){
$chunks = preg_split("/(<span.*?\/span>)/ms", $s, - 1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($chunks as $c) {
if (strpos($c, "<span") !== 0){
$tmp = $c;
$c = str_ireplace($keyword, "<span class="text-danger">".$keyword. "</span>", $c);
$s = str_ireplace($tmp, $c, $s);
}
}
}else{
$s = str_ireplace($keyword, "<span class="text-danger"> ".$keyword. "</span>", $s);
}
}
return $s;
}
问题
$post['message_fmt'] = str_replace($haya_post_info_usernames_fmt[0][$i], '<a href="' . url('user-' . $haya_post_info_user['uid']) . '" target="_blank" class="haya-post-info-at"><em>@' . $haya_post_info_user['username'] . '</em></a>', $post['message_fmt']);
$post['message'] = str_replace($haya_post_info_usernames[0][$i], '<a href="' . url('user-' . $haya_post_info_user['uid']) . '" target="_blank" class="haya-post-info-at"><em>@' . $haya_post_info_user['username'] . '</em></a>', $post['message']);
如果有两个以上相似的用户名 :admin 跟 admi 同时被@admin@admi@admin@admi @admin@admi 会出现匹配混乱
str_replace()函数里的$haya_post_info_usernames[0][$i]循环一次就替换$post['message']所有的@admin@admi 多次循环导致混乱
$post['message'] = str_replace($haya_post_info_usernames[0][$i], '<a href="' . url('user-' . $haya_post_info_user['uid']) . '" target="_blank" class="haya-post-info-at"><em>@' . $haya_post_info_user['username'] . '</em></a>', $post['message']);
参考
在帖子快速回复时,@用户名后面的内容会全部丢失。
【原因】
1. 插件匹配从@开始直到[^\s|\/|:|@](空白和换行、/、:、@几个符号)中止,并替换匹配的内容为用户链接。
2. 快速回复时空格为 ,换行为<br>,不在匹配范围内。
3. 所以导致后面的内容一并被匹配替换,导致内容丢失。
【修复】
1. 打开/plugin/haya_post_info/hook/post_post_end.php
2. 找到如下两行:
|
1
2
|
preg_match_all('/@([^\s|\/|:|@]+)/', $post['message_fmt'], $haya_post_info_usernames_fmt); preg_match_all('/@([^\s|\/|:|@]+)/', $post['message'], $haya_post_info_usernames); |
3. 替换为:
|
1
2
|
preg_match_all('/@([^\s|\/|:|@|<|>| ]+)/', $post['message_fmt'], $haya_post_info_usernames_fmt); preg_match_all('/@([^\s|\/|:|@|<|>| ]+)/', $post['message'], $haya_post_info_usernames); |