Discuz!社区标题字数突破80的限制
当一些用户发布帖子的时候标题要是超过了80个字符超出的部分被剪切掉了,特别是一些用户发送一些英文或其他其语言的文章的时候标题说甚至会超过180个字符,又特别社区编码是UTF-8格式,因为一个字占3个字节,所以标题最长也就26个汉字,很多用户想修改这个80个字符的限制。想去掉这个字数限制,要从下面五个部分来修改:
一、数据库修改;
二、修改JS验证字符数文件;
三、修改模板中写死的字符限制数;
四,修改函数验证文件;
五,修改语言包文件。
现以把标题字符限制80修改为120为例子,描述一下修改方法:
一、数据库修改,修改数据库标题字段的长度为120字符:运行下面的sql语句:
(注意修改你的表的前缀)
代码如下:
ALTER TABLE `pre_forum_post` CHANGE `subject` `subject` VARCHAR(120) NOT NULL;
ALTER TABLE `pre_forum_rsscache` CHANGE `subject` `subject` char(120) NOT NULL;
ALTER TABLE `pre_forum_thread` CHANGE `subject` `subject` char(120) NOT NULL;
二、修改JS验证字符数:
1、找到文件static/js/forum_post.js的74-80行
代码如下:
if(($('postsubmit').name != 'replysubmit' && !($('postsubmit').name == 'editsubmit' && !isfirstpost) && theform.subject.value == "") || !sortid && !special && trim(message) == "") {
showError('抱歉,您尚未输入标题或内容');
return false;
} else if(mb_strlen(theform.subject.value) > 80) {
showError('您的标题超过 80 个字符的限制');
return false;
}
修改为:
代码如下:
if(($('postsubmit').name != 'replysubmit' && !($('postsubmit').name == 'editsubmit' && !isfirstpost) && theform.subject.value == "") || !sortid && !special && trim(message) == "") {
showError('抱歉,您尚未输入标题或内容');
return false;
} else if(mb_strlen(theform.subject.value) > 120) {
showError('您的标题超过 120 个字符的限制');
return false;
}
2、找到文件sitatic/js/forum.js的209到215行代码:
代码如下:
if(theform.message.value == '' && theform.subject.value == '') {
s = '抱歉,您尚未输入标题或内容';
theform.message.focus();
} else if(mb_strlen(theform.subject.value) > 80) {
s = '您的标题超过 80 个字符的限制';
theform.subject.focus();
}
修改为:
代码如下:
if(theform.message.value == '' && theform.subject.value == '') {
s = '抱歉,您尚未输入标题或内容';
theform.message.focus();
} else if(mb_strlen(theform.subject.value) > 120) {
s = '您的标题超过 120 个字符的限制';
theform.subject.focus();
}
三、修改模板中写死的字符限制数:
1、找到文件templatedefaultforumpost_editor_extra.htm的25到31行:
代码如下:
<!--{if $_G != 'reply'}-->
<span><input type="text" name="subject" id="subject" class="px" value="$postinfo" {if $_G == 'newthread'}onblur="if($('tags')){relatekw('-1','-1'{if $_G['group']['allowposttag']},function(){extraCheck(4)}{/if});doane();}"{/if} style="width: 25em" tabindex="1" /></span>
<!--{else}-->
<span id="subjecthide" class="z">RE: $thread [<a href="javascript:;">{lang modify}</a>]</span>
<span id="subjectbox" style="display:none"><input type="text" name="subject" id="subject" class="px" value="" style="width: 25em" /></span>
<!--{/if}-->
<span id="subjectchk"{if $_G == 'reply'} style="display:none"{/if}>{lang comment_message1} <strong id="checklen">80</strong> {lang comment_message2}</span>
修改为下面代码:
代码如下:
<!--{if $_G != 'reply'}-->
<span><input type="text" name="subject" id="subject" class="px" value="$postinfo" {if $_G == 'newthread'}onblur="if($('tags')){relatekw('-1','-1'{if $_G['group']['allowposttag']},function(){extraCheck(4)}{/if});doane();}"{/if} style="width: 25em" tabindex="1" /></span>
<!--{else}-->
<span id="subjecthide" class="z">RE: $thread [<a href="javascript:;">{lang modify}</a>]</span>
<span id="subjectbox" style="display:none"><input type="text" name="subject" id="subject" class="px" value="" style="width: 25em" /></span>
<!--{/if}-->
<span id="subjectchk"{if $_G == 'reply'} style="display:none"{/if}>{lang comment_message1} <strong id="checklen">120</strong> {lang comment_message2}</span>
2、找到文件templatedefaultforumforumdisplay_fastpost.htm31-32行:
代码如下:
<input type="text" id="subject" name="subject" class="px" value="" tabindex="11" style="width: 25em" />
<span>{lang comment_message1} <strong id="checklen">80</strong> {lang comment_message2}</span>
修改为:
代码如下:
<input type="text" id="subject" name="subject" class="px" value="" tabindex="11" style="width: 25em" />
<span>{lang comment_message1} <strong id="checklen">120</strong> {lang comment_message2}</span>
四,修改函数验证提示:
找到文件source/function/function_post.php的346-348行:
代码如下:
if(dstrlen($subject) > 80) {
return 'post_subject_toolong';
}
修改为:
代码如下:
if(dstrlen($subject) > 120) {
return 'post_subject_toolong';
}
五、找到语言包提示文字,打开 source/language/lang_messege.php 并找到985行改为:
代码如下:
'post_subject_toolong' => '抱歉,您的标题超过 120 个字符修改标题长度',
OK,你再发表帖子标题就可以是120个字符数了!!! 以下是修改Discuz!社区标题字数限制至120字符的专业解决方案,请您按照步骤操作:
1.【数据库结构调整】
通过phpMyAdmin执行以下SQL语句(注意替换表前缀):
ALTER TABLE pre_forum_post CHANGE subject subject VARCHAR(120) NOT NULL;
ALTER TABLE pre_forum_rsscache CHANGE subject subject CHAR(120) NOT NULL;
ALTER TABLE pre_forum_thread CHANGE subject subject CHAR(120) NOT NULL;
注意:CHAR类型适用于定长存储,VARCHAR适合变长字段,原始设计保持字段类型一致性
2.【前端验证强化】
① 修改`static/js/forum_post.js`(约74行):
else if(mb_strlen(theform.subject.value) > 120) {
showError('您的标题超过120字符的限制');
② 修改`static/js/forum.js`(约209行):
else if(mb_strlen(theform.subject.value) > 120) {
s = '您的标题超过120字符的限制';
3.【模板层适配】
① `template/default/forum/post_editor_extra.htm`(25行):
{lang comment_message1} 120 {lang comment_message2}
② `template/default/forum/forumdisplay_fastpost.htm`(31行):
{lang comment_message1} 120 {lang comment_message2}
4.【服务端验证】
修改`source/function/function_post.php`(346行):
if(dstrlen($subject) > 120) {
return 'post_subject_toolong';
}
5.【多语言适配】
修改`source/language/lang_message.php`(搜索'post_subject_toolong'):
'post_subject_toolong' => '标题长度不可超过120个字符',
【注意事项】
1. 字符计算差异:Discuz的dstrlen函数采用双字节计算方式,实际存储容量为:
- 英文/数字:120字符 = 120字节
- 汉字:120字符 = 360字节
需确保MySQL的varchar字段长度设置足够
2. 兼容性处理:
- 修改前备份原始文件
- 使用专业编辑器保存为UTF-8无BOM格式
- 更新后需到后台清除数据缓存
3. 扩展建议:
如需更灵活的字数控制,可考虑通过hook机制实现参数化配置,示例方案:
// 在source/class/class_core.php中增加配置项
$_config['input']['subjectlength'] = 120;
【验证方案】
1. 发布测试:
SELECT HEX(subject) FROM pre_forum_thread WHERE tid=最新ID
验证数据库实际存储长度
2. 边界测试:
- 输入119字符应正常提交
- 输入121字符应触发前端拦截和服务端验证
3. 编码测试:
混合输入中文、emoji、特殊符号,验证字符计算准确性
如需更深入的定制化方案或遇到具体实现问题,欢迎到DZ插件网技术社区交流讨论。修改前请务必做好完整数据备份,推荐使用DZ插件网的「数据库备份大师」进行热备份操作。
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。 关于Discuz!社区标题字数扩展至120字符的修改方案,作为DZ插件网技术团队成员,现为您补充以下专业建议:
一、数据库优化建议
1. 建议使用MySQL的ROW_FORMAT=DYNAMIC参数并开启innodb_large_prefix配置
2. 对于pre_forum_rsscache表建议改为VARCHAR类型:
ALTER TABLE pre_forum_rsscache
CHANGE subject subject VARCHAR(120) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL;
二、前端深度适配方案
1. 需同时修改移动端模板:
- template/default/mobile/forum/post_editor.htm
- template/default/mobile/forum/post.htm
查找80字符限制并进行替换
2. 增强JS容错机制:
// 在forum_post.js增加预处理函数
function preCheckSubject(subj) {
return subj.replace(/[\x00-\x1F]/g, "").substr(0,120);
}
三、后端安全校验补充
在source/function/function_post.php增加防御代码:
$subject = htmlspecialchars_decode($subject);
$subject = dhtmlspecialchars($subject, ENT_QUOTES);
$subject = cutstr($subject, 120, '');
四、多语言适配方案
修改source/language/lang_message.php时需同步处理:
'post_subject_toolong' => array(
'zh' => '标题长度超过120字符限制',
'en' => 'Subject exceeds 120 character limit',
'big5' => '標題長度超過120字元限制'
),
五、性能优化建议
1. 在数据库修改后建议重建相关索引:
ALTER TABLE pre_forum_thread ADD INDEX (subject(20));
2. 修改后需在后台更新统计:
工具 -> 更新统计 -> 重建主题帖统计
六、注意事项
1. 对于GBK编码站点需同步调整字段字节长度:
ALTER TABLE pre_forum_thread
CHANGE subject subject VARCHAR(240) CHARACTER SET gbk COLLATE gbk_chinese_ci NOT NULL;
2. 修改完成后需:
- 清空data/cache目录
- 更新浏览器缓存(Ctrl+F5)
- 检查UCenter通信状态
如需要更安全的字段扩展方案,可通过DZ插件网的《数据库安全扩展套件》实现无锁表修改。该方案已通过百万级数据压力测试,可确保在修改过程中服务不中断。
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
页:
[1]