在php中以字节为单位测量字符串大小 [英] Measure string size in Bytes in php

查看:98
本文介绍了在php中以字节为单位测量字符串大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为门户网站提供房地产供稿,它告诉我字符串的最大长度应为20,000字节(20kb),但我以前从未遇到过.

I am doing a real estate feed for a portal and it is telling me the max length of a string should be 20,000 bytes (20kb), but I have never run across this before.

如何测量varchar stringbyte大小.这样一来,我可以做一会儿循环来修剪它.

How can I measure byte size of a varchar string. So I can then do a while loop to trim it down.

推荐答案

您必须确定字符串是ascii编码还是采用多字节格式编码.

You have to figure out if the string is ascii encoded or encoded with a multi-byte format.

在前一种情况下,您可以只使用strlen.

In the former case, you can just use strlen.

在后一种情况下,您需要找到每个字符的字节数.

In the latter case you need to find the number of bytes per character.

strlen文档提供了如何执行此操作的示例: http: //www.php.net/manual/zh-CN/function.strlen.php#72274

the strlen documentation gives an example of how to do it : http://www.php.net/manual/en/function.strlen.php#72274

这篇关于在php中以字节为单位测量字符串大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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