限制包含HTML标签的文本的输入长度 [英] Limit input length of text that contains HTML tags

查看:691
本文介绍了限制包含HTML标签的文本的输入长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php网站,我可以管理文章。在添加新文章表单上,有一个富文本框(允许HTML输入),我想限制字符输入计数。我检查服务器端,使用 strlen() ­ Docs 方法。



问题是 strlen 似乎给了一个太大的数字。我尝试使用 html_entity_decode() ­ Google文档 将html标签从字符串中取出,但字符串长度仍然是错误的。

html_entity_decode 只解码HTML实体,它不会忽略HTML标签。尝试:

  strlen(strip_tags(html_entity_decode($ string))); 

或者等值多字节:

  mb_strlen(strip_tags(html_entity_decode($ string)),'auto'); 


I have a php web sites in wich I can manages articles. On the Add a new article form, there is a rich-text-box (allows HTML input) that I'd like to limit the character input count. I check on the server side so using the strlen()­Docs method.

The problem is strlen seems to give a number that is way too big. I tried to use html_entity_decode()­Docs to get the html tags out of the string but still the string length resulting seems to be wrong.

解决方案

html_entity_decode only decodes HTML entities, it doesn't ignore HTML tags. Try:

strlen(strip_tags(html_entity_decode($string)));

Or the multi-byte equivalent:

mb_strlen(strip_tags(html_entity_decode($string)), 'auto');

这篇关于限制包含HTML标签的文本的输入长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆