注册 登录
编程论坛 PHP技术论坛

[分享][讨论][求助]请教:如何用PHP或JS控制:标题限制为4-20个汉字,内容10-3000个

qiangzai8536 发布于 2007-02-14 01:00, 3088 次点击

如何用PHP或JS控制:标题限制为4-20个汉字,内容10-3000个汉字呢?此文本大致如下:谢谢!

<html>
<head>
</head>
<body>

<form method='POST' action='fbjypx_yjzlb.php?action=login'>
<table border="0" align="center" width="750" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" class="f2">密&nbsp;&nbsp;码:<input type="text" name="mm" size="20" onMouseOver = "this.style.backgroundColor = '#E5F0FF'" onMouseOut = "this.style.backgroundColor = ''">
</td>
</tr>
<tr>
<td width="100%" class="f2">标&nbsp;&nbsp;题:<input type="text" name="bt" size="40" onMouseOver = "this.style.backgroundColor = '#E5F0FF'" onMouseOut = "this.style.backgroundColor = ''">
<font class="f1">(23个汉字以内)</font></td>
</tr>
<br>
<tr>
<td width="100%" class="f2">内&nbsp;&nbsp;容:<font class="f1">(3000个汉字以内)</font></td>
</tr>
<tr>
<td width="100%" >
<textarea rows="20" name="nr" cols="105" onMouseOver = "this.style.backgroundColor = '#E5F0FF'" onMouseOut = "this.style.backgroundColor = ''"></textarea></td>
</tr>
<tr>
<td width="100%" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" value="提&nbsp;交">&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="重&nbsp;来"></td>
</tr>
</table>
</form>
</body>
</html>

2 回复
#2
JavaEE52007-05-25 10:02
strlen 获取字符串长度
substr 截取字符串
#3
lmhllr2007-05-25 12:35

PHP原有函数对中英混合的支持不大好

如果有mb_扩展直接用就可以了

没有的话用这个函数

function strSplit($s, $len) {
$end = '…';
$result = '';
$strLen = strlen($s);
if ($strLen <= $len) {
return $s;
}
$len -= 2;
for ($i=0; $i<$len && $i<$strLen; $i++) {
$c = $s[$i];
if (ord($c) < 0x80) {
$result .= $c;
} elseif ($i+1<$len) {
$result .= $s[$i++] . $s[$i];
}
}
return ($i < $strLen) ? ($result . $end) : $result;
}

echo strSplit('1234567', 10), '<br />';
echo strSplit('1234567890', 10), '<br />';
echo strSplit('1234中文567890abcdefghijkl', 10), '<br />';
echo strSplit('全部都是中文', 10), '<br />';
echo strSplit('全a部b都c是d中e文', 10), '<br />';

1