Forgot password?
 Create new account
View 141|Reply 17

[已解决]当搜索出来的帖子有 html ……

[Copy link]

700

Threads

110K

Posts

910K

Credits

Credits
94177
QQ

Show all posts

kuing Posted at 2024-3-5 21:25:58 |Read mode
Last edited by kuing at 2024-3-5 23:08:00在论坛搜索时,如果搜出来的帖子1楼有 html 代码,由于每个条目只截取开头一段,就会缺失结尾的 html,导致整个页面有可能错乱,如何改进?@hbghlyj

700

Threads

110K

Posts

910K

Credits

Credits
94177
QQ

Show all posts

 Author| kuing Posted at 2024-3-5 22:34:09
gitee.com/Discuz/DiscuzX/blob/v3.5/upload/sou … earch_forum.php#L129
L129 的
  1. $threadlist[$post['tid']]['message'] = bat_highlight(threadmessagecutstr($threadlist[$post['tid']], $post['message'], 200), $keyword);
Copy the Code

就是截取 200 个字符吗?

如果是这里,那如果想将内容的 < 和 > 替换成 &lt; 和 &gt; 应该怎么做?

700

Threads

110K

Posts

910K

Credits

Credits
94177
QQ

Show all posts

 Author| kuing Posted at 2024-3-5 22:49:24
hbghlyj 发表于 2024-3-5 22:44
Discuz!开发之字符串处理函数dhtmlspecialchars介绍原创CSDN博客
9 Apr 2017 — dhtmlspecialchars实际上是 ...
那就是套一层 dhtmlspecialchars 就行了?即 L129 改成下面这样?:
  1. $threadlist[$post['tid']]['message'] = bat_highlight(threadmessagecutstr($threadlist[$post['tid']], dhtmlspecialchars($post['message']), 200), $keyword);
Copy the Code


Comment

试了一下,好像可以了😊  Posted at 2024-3-5 23:07

700

Threads

110K

Posts

910K

Credits

Credits
94177
QQ

Show all posts

 Author| kuing Posted at 2024-3-5 21:43:47
我主要不知道显示搜索结果的相关代码在哪……

我觉得搜索结果过滤掉 html 代码比较稳妥

3148

Threads

8498

Posts

610K

Credits

Credits
66193
QQ

Show all posts

hbghlyj Posted at 2024-3-5 21:58:53
kuing 发表于 2024-3-5 13:43
我主要不知道显示搜索结果的相关代码在哪……

我觉得搜索结果过滤掉 html 代码比较稳妥 ...
我没权限。。好像应该是
source /module /search /search_forum.php

700

Threads

110K

Posts

910K

Credits

Credits
94177
QQ

Show all posts

 Author| kuing Posted at 2024-3-5 22:06:09
hbghlyj 发表于 2024-3-5 21:58
我没权限。。好像应该是
source /module /search /search_forum.php
你以前不是改过搜索功能的嘛,我以为你已经很熟悉相关文件啦
gitee.com/Discuz/DiscuzX/blob/v3.5/upload/sou … rch/search_forum.php
好吧我慢慢看吧

3148

Threads

8498

Posts

610K

Credits

Credits
66193
QQ

Show all posts

hbghlyj Posted at 2024-3-5 22:16:43
kuing 发表于 2024-3-5 14:06
你以前不是改过搜索功能的嘛,我以为你已经很熟悉相关文件啦
我没权限看不到代码。。只能盲猜

700

Threads

110K

Posts

910K

Credits

Credits
94177
QQ

Show all posts

 Author| kuing Posted at 2024-3-5 22:18:53
hbghlyj 发表于 2024-3-5 22:16
我没权限看不到代码。。只能盲猜
你的网站上不是有我之前的论坛备份嘛,都删了吗?

3148

Threads

8498

Posts

610K

Credits

Credits
66193
QQ

Show all posts

hbghlyj Posted at 2024-3-5 22:32:44
kuing 发表于 2024-3-5 14:18
你的网站上不是有我之前的论坛备份嘛,都删了吗?
当时更新的代码没有更新那边,所以还是和官方一样的

3148

Threads

8498

Posts

610K

Credits

Credits
66193
QQ

Show all posts

hbghlyj Posted at 2024-3-5 22:43:01
kuing 发表于 2024-3-5 14:34
L129 的 ... 就是截取 200 个字符吗?
可以先检视threadmessagecutstr的输出,看看输出什么

3148

Threads

8498

Posts

610K

Credits

Credits
66193
QQ

Show all posts

hbghlyj Posted at 2024-3-5 22:44:50

相关资料

Discuz!开发之字符串处理函数dhtmlspecialchars介绍原创CSDN博客
9 Apr 2017 — dhtmlspecialchars实际上是对PHP内置函数htmlspecialchars的二次封装和补充,使得不仅可以处理字符串还可以递归处理数组;它的作用是是可以把一个数组或字符串中的字符转化为html实体,可以防止页面的跨站问题,那么我们看到他的转换就是将‘&’,‘”’,‘<’,‘>’转化为'&', '"', '<', '>'。但是这里面会有一些问题,如‘& #x5FD7;’这样的16进制的html字符,为了防止这样的字符被错误转译,所以又使用了正则进行匹配,把这样的字符又转换回来;一般使用在内容输出到界面之前处理要输出的内容;


700

Threads

110K

Posts

910K

Credits

Credits
94177
QQ

Show all posts

 Author| kuing Posted at 2024-3-5 22:55:12
hbghlyj 发表于 2024-3-5 22:44
Discuz!开发之字符串处理函数dhtmlspecialchars介绍原创CSDN博客
9 Apr 2017 — dhtmlspecialchars实际上是 ...
链接里的代码显示有问题,第二个 array('&amp;', '&quot;', '&lt;', '&gt;') 也显示成 array('&', '"', '<', '>') 了。
正确的是:
  1. function dhtmlspecialchars($string, $flags = null) {
  2.         if(is_array($string)) {
  3.                 foreach($string as $key => $val) {
  4.                         $string[$key] = dhtmlspecialchars($val, $flags);
  5.                 }
  6.         } else {
  7.                 if($flags === null) {
  8.                         $string = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string);
  9.                         if(strpos($string, '&amp;#') !== false) {
  10.                                 $string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $string);
  11.                         }
  12.                 } else {
  13.                         if(PHP_VERSION < '5.4.0') {
  14.                                 $string = htmlspecialchars($string, $flags);
  15.                         } else {
  16.                                 if(strtolower(CHARSET) == 'utf-8') {
  17.                                         $charset = 'UTF-8';
  18.                                 } else {
  19.                                         $charset = 'ISO-8859-1';
  20.                                 }
  21.                                 $string = htmlspecialchars($string, $flags, $charset);
  22.                         }
  23.                 }
  24.         }
  25.         return $string;
  26. }
Copy the Code

700

Threads

110K

Posts

910K

Credits

Credits
94177
QQ

Show all posts

 Author| kuing Posted at 2024-3-6 13:35:51
又试了一下回帖置顶,这次正常。

就是不知为何上次这帖就不正常。

3148

Threads

8498

Posts

610K

Credits

Credits
66193
QQ

Show all posts

hbghlyj Posted at 2024-3-6 16:03:51
kuing 发表于 2024-3-6 05:35
又试了一下回帖置顶,这次正常。

就是不知为何上次这帖就不正常。
上次有删帖导致楼层乱了?

700

Threads

110K

Posts

910K

Credits

Credits
94177
QQ

Show all posts

 Author| kuing Posted at 2024-3-6 16:15:29
hbghlyj 发表于 2024-3-6 16:03
上次有删帖导致楼层乱了?
本帖你不是也删了“自动关闭标签”的回帖吗,也没乱啊

3148

Threads

8498

Posts

610K

Credits

Credits
66193
QQ

Show all posts

hbghlyj Posted at 2024-3-6 17:53:20
kuing 发表于 2024-3-6 08:15
本帖你不是也删了“自动关闭标签”的回帖吗,也没乱啊
没删啊我只是分割了楼层到新主题

Comment

好吧,完全没注意到有新帖  Posted at 2024-3-6 18:11

手机版Mobile version|Leisure Math Forum

2025-4-21 01:45 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list