Forgot password?
 Create new account
View 190|Reply 31

[已解决] 点评中的[color=]

[Copy link]

3148

Threads

8489

Posts

610K

Credits

Credits
66148
QQ

Show all posts

hbghlyj Posted at 2025-2-24 00:28:43 |Read mode
Last edited by hbghlyj at 2025-3-2 15:53:00ok

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

kuing Posted at 2025-2-24 03:25:19
Last edited by kuing at 2025-2-24 03:51:00确实是有点奇怪,如果你在本页右键查看网页源代码,你会发现代码是正常的:
  1. 点评中的<font color="Red">红色文字</font>测试&nbsp;
Copy the Code


本回帖特意加了一个红色字来对比。
从源代码来看,语法是完全一样的。
但如果再仔细看,你会发现 chrome 的源代码页面里,虽然语法一样,但字母的颜色是不一样的!
截图对比:
QQ20250224-033036.png
正常的 font、color、Red 这三个单词的颜色都不同,但前面的却一样颜色!
浏览器将整个 font color="Red" 当成了一个长文本?所以后面才会出现 </font color="Red"> 来关闭它?
太奇怪了,为什么浏览器不认它?难道点评里的 font color 中间的空格不是普通空格?

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

kuing Posted at 2025-2-24 03:57:38
还真是空格问题!我真聪明😂

检查元素,右键“以 HTML 格式修改”,就会看到是这样的:
QQ20250224-035619.png

<font&nbsp;color="red">

3148

Threads

8489

Posts

610K

Credits

Credits
66148
QQ

Show all posts

 Author| hbghlyj Posted at 2025-2-24 04:58:59
kuing 发表于 2025-2-23 19:57
还真是空格问题!我真聪明

应该是PHP替换的先后次序问题:
先替换BBcode为HTML再替换空格为&nbsp;则会出错
先替换空格为&nbsp;再替换BBcode为HTML则不出错

3148

Threads

8489

Posts

610K

Credits

Credits
66148
QQ

Show all posts

 Author| hbghlyj Posted at 2025-2-24 05:10:54
替换BBcode为HTML的位置好像在source\module\forum\forum_misc.php
  1. $comment['comment'] = str_replace(array('[b]', '[/b]', '[/color]'), array('<b>', '</b>', '</font>'), preg_replace("/\[color=([#\w]+?)\]/i", "<font color=\"\\1\">", $comment['comment']));
Copy the Code

3148

Threads

8489

Posts

610K

Credits

Credits
66148
QQ

Show all posts

 Author| hbghlyj Posted at 2025-2-24 05:16:08
替换空格为&nbsp;的位置好像在template\default\forum\viewthread_node_body.htm
  1. {echo nl2br(str_replace(' ', "\xc2\xa0",$comment[comment]));}&nbsp;
Copy the Code

和template\default\forum\comment_more.htm
  1. {echo nl2br(str_replace(' ', "\xc2\xa0",$comment[comment]));}&nbsp;
Copy the Code

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

kuing Posted at 2025-2-24 13:43:35
Last edited by kuing at 2025-2-24 14:35:00
hbghlyj 发表于 2025-2-24 04:58
应该是PHP替换的先后次序问题:
先替换BBcode为HTML再替换空格为&nbsp;则会出错
先替换空格为&nbsp;再替换 ...
点评中为什么要替换空格为 &nbsp; ?是否多余?

Comment

test Red Blue B test  Posted at 2025-2-24 14:46
测试: B 多     空     格      😁  Posted at 2025-2-24 14:49
哦,不作替换,多个空格就变成一个空格?  Posted at 2025-2-24 14:54
tab        tab  Posted at 2025-2-24 20:13

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

kuing Posted at 2025-2-24 14:52:07
Last edited by kuing at 2025-2-24 15:55:00
hbghlyj 发表于 2025-2-24 05:16
替换空格为&nbsp;的位置好像在template\default\forum\viewthread_node_body.htm

和template\default\foru ...
将这两个文件的这两句都改成了
  1. {echo nl2br(str_replace('  ', "\xc2\xa0\xc2\xa0",$comment[comment]));}&nbsp;
Copy the Code

(两个空格换成两个 \xc2\xa0)
颜色已经没问题了😃

PS、
viewthread_node_body.htm 是正常浏览时显示的;
comment_more.htm 是新增点评时立刻显示的。

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

kuing Posted at 2025-2-24 15:02:08
咦?我看了官方的模板,那里那两句原本只是
  1. &nbsp;$comment[comment]&nbsp;
Copy the Code

替换操作是你之前加上去的吗???😳
你为了让点评中的空格和换行正常显示而为之?
咳,搞了半天,原来这 bug 是你弄出来的啊

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

kuing Posted at 2025-2-24 15:27:09
Last edited by kuing at 2025-2-24 16:18:00官方模板对换行和多空格的替换处理通常是这样的:
  1. nl2br(str_replace(array("\t", '   ', '  '), array('&nbsp; &nbsp; &nbsp; &nbsp; ', '&nbsp; &nbsp;', '&nbsp;&nbsp;'), $message));
Copy the Code

(这句出自 /source/function/function_discuzcode.php)
点评那里是否可以抄它?

按我理解,该语句在处理连续空格的逻辑是,在处理完 \t(tab)后:
先替换连续 3 个空格为 &nbsp; &nbsp;(注意这两个 &nbsp; 之间有一个普通空格)
再替换连续 2 个空格为 &nbsp;&nbsp;
这样就替换完所有连续空格,而单个空格的不会处理。

如果这理解没错,那为什么要分两步呢?直接替换连续 2 个空格为 &nbsp;&nbsp; 不行吗?只是为了节省一些 &nbsp; ?

一个 空格
两个  空格
三个   空格
四个    空格
五个     空格
六个      空格
超长                                                                                                                                                                                                                                    空格

Comment

一个 空格
两个  空格
三个   空格
四个    空格
五个     空格
六个      空格  Posted at 2025-2-24 15:58
超长                                                                                                                                                                                           空格  Posted at 2025-2-24 16:26

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

kuing Posted at 2025-2-24 16:17:47

续 10#

Last edited by kuing at 2025-2-24 20:06:00我似乎明白了,分两步并不是为了节省 &nbsp;
如果直接替换连续 2 空格为 &nbsp;&nbsp; 那如果连续超多空格,就会出现一长串不间断的 &nbsp; ,这会导致长文本的自然断行出问题。
而如果用官方模板的方法,就不会出现这情况,因为 3 个空格替换时中间有一个普通空格,长文本就可以在这些普通空格处断行。

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

kuing Posted at 2025-2-24 18:35:27
对了,手机版现在还是默认的,换行与多空格都无效。

我想问,是不是只要修改某个 php,比如你说的 source\module\forum\forum_misc.php,那就不用改那两个 htm,同时手机版也有效?

Comment

是的!  Posted at 2025-2-24 18:37

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

kuing Posted at 2025-2-24 19:02:05

  1. $comment['comment'] = str_replace(array('[b]', '[/b]', '[/color]'), array('<b>', '</b>', '</font>'), preg_replace("/\[color=([#\w]+?)\]/i", "<font color=\"\\1\">", $comment['comment']));
Copy the Code

的 php 不止一个,除了你提到的
source\module\forum\forum_misc.php
之外,还有
source\class\table\table_forum_postcomment.php

它们分别管什么呢?

Comment

哦,缓存……  Posted at 2025-2-24 19:18
那如果只改一个,会出现什么情况呢?  Posted at 2025-2-24 19:20

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

kuing Posted at 2025-2-24 19:51:59
如果要改,那是不是将那句改成
  1. $comment['comment'] = nl2br(str_replace(array("\t", '   ', '  ', '[b]', '[/b]', '[/color]'), array('&nbsp; &nbsp; &nbsp; &nbsp; ', '&nbsp; &nbsp;', '&nbsp;&nbsp;', '<b>', '</b>', '</font>'), preg_replace("/\[color=([#\w]+?)\]/i", "<font color=\"\\1\">", $comment['comment'])));
Copy the Code

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

kuing Posted at 2025-2-24 20:11:09
已将
source\module\forum\forum_misc.php
source\class\table\table_forum_postcomment.php
的那句都改成了楼上的。

然后取消 8# 的修改,即
viewthread_node_body.htm
comment_more.htm
那两句还原为 $comment[comment]&nbsp;

再到后台更新缓存。

现在好像旧点评没有换行和连续空格,但只要添加新点评就会更新。

Comment

手机版也有换行多空格啦😃  Posted at 2025-2-24 20:18
有没有办法批量处理旧点评?  Posted at 2025-2-24 20:22
换新域名后好像旧点评也有换行啥的了  Posted at 2025-3-2 22:24

3148

Threads

8489

Posts

610K

Credits

Credits
66148
QQ

Show all posts

 Author| hbghlyj Posted at 2025-3-2 20:32:01
kuing 发表于 2025-2-24 12:11
已将
source\module\forum\forum_misc.php
source\class\table\table_forum_postcomment.php
用同样的方法添加了点评中的链接解析

手机版Mobile version|Leisure Math Forum

2025-4-20 12:21 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list