substr() 返回错误的字符数 [英] substr() return incorrect number of characters

查看:30
本文介绍了substr() 返回错误的字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

substr() 返回 28 个字符而不是 25 个.

substr() is returning 28 characters instead of 25.

$nature_goods 是输入栏,用户可以在此输入包含特殊字符的字符串.

$nature_goods is the input field, where user can enter string containing special characters.

代码

$nature_goods =  "Nature quantityyy of goods is nice 120pcs 1*X23X24898";
echo strlen($nature_goods);
echo "<br>";
echo substr($nature_goods, 0,25);
echo "<br>";
echo strlen(substr($nature_goods, 0,25));
echo "<br>";
echo substr($nature_goods, 25,50);
echo "<br>";
echo strlen(substr($nature_goods, 25,50));
echo "<br>";

输出

53
Nature quantityyy of good
25
s is nice 120pcs 1*X23X24898
28

我尝试了 mb_substr()mb_strlen(),但我没有看到任何多字节字符串造成问题.有人能指出错误吗?

I tried mb_substr() and also mb_strlen(), but I do not see any multibyte string creating a problem. Can someone point out the mistake?

推荐答案

您使用的 substr 不正确.

You are using substr incorrectly.

您正在使用传递给函数的最后一个值作为终点而不是长度,如获取 25 到 50 之间的字符,而不是使用应该使用的 substr,正如您所写的那样, 跳过前 25 个字符后抓取 50 个字符.

You are using the last value passed to the function as an end point rather than a length, as in grab the characters between 25 and 50, instead of using substr as it supposed to be used, which, as you have written it, is grab 50 characters after skipping the first 25 characters.

最后的函数调用应该是 substr($nature_goods, 25, 25);如果您希望收到 25 个字符作为回报.

The final function call should be substr($nature_goods, 25, 25); if you are expecting to receive 25 characters in return.

substr 的预期值:

Values expected for substr:

substr ( string $string , int $start , int $length )

substr ( string $string , int $start , int $length )

此处提供完整文档:

http://php.net/manual/en/function.substr.php

这篇关于substr() 返回错误的字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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