使用PHP限制HTML文本中特定数字的字符

Kos*_*tas 0 php character maxlength ckeditor

我正在使用CKEditor,我找到/修改了一个计算字符的插件.

我有高级和基本用户.基本用户限制为1000个字符,溢价是无限制的,但基本可以写全文用于预览/测试等(它是客户规范,所以不能改变它).

当我在CKEditor中显示例如1500个1000个字符时,我想保存在DB 1500字符中但在文本输出中只显示其中的1000个字符.

但strlen和相关函数将HTML标记计为字符,我不希望这样.我也不想剥离它们,因为我会丢失格式.

有没有办法确保应用限制但是所有标签都将保留(在PHP中)

谢谢...

Dut*_*432 5

尝试

$theHTML='<h2>Hello!</h2>';
$length = strlen ( strip_tags($theHTML) ); //Should be 6
echo "The non-HTML length is: $length";
Run Code Online (Sandbox Code Playgroud)

这样只会剥离标记以进行计数.标签永远不会丢失.

更新

根据webbiedave他的建议,它确实应该是

$theHTML='<h2>Hello!</h2>';
$length = strlen ( shtml_entity_decode(strip_tags($theHTML)) ); //Should be 6

//This will not trigger since only the text "Hello!" is only 6 chars.    
if ($length > 10) die('ERROR'); 

echo $theHTML; //Will echo full HTML, even though we checked the length without HTML.
Run Code Online (Sandbox Code Playgroud)

  • 你应该真的做`html_entity_decode(strip_tags($ theHTML))`来减少实体,例如`````````` (2认同)