php判断输入不超过mysql的varchar字段的长度范围_php技巧

WBOY
Release: 2016-05-17 09:18:08
Original
890 people have browsed it

但是如果在utf-8编码下,一个汉字是占3个字符长度的,比如字符串$str=”你好啊!!”;

如果你用strlen函数来判断,长度是11,正好超过了varchar的长度,但实际上确不是这样,如果直接到phpmyadmin里面执行insert语句,这条字符串是可以插入的!

对于数据库来说,它的长度是5,那么我们如何用PHP来得到这个长度呢?使用iconv_strlen()函数!

echo iconv_strlen($str,'utf-8′);

注意第二个参数,是当前字符集,这样根据不同的字符集你得到的结果都是根据一个字符占一个长度计算来的!

上面的语句,将输出5,怎么样,你会判断了吗?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!