duanju8308 2017-03-16 04:44
浏览 81
已采纳

substr()返回不正确的字符数

substr() is returning 28 characters instead of 25.

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

Code

$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>";

Output

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

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?

  • 写回答

2条回答 默认 最新

  • douyong1908 2017-03-16 04:54
    关注

    You are using substr incorrectly.

    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.

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

    Values expected for substr:

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

    Full documentation here:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 github符合条件20分钟秒到账,github空投 提供github账号可兑换💰感兴趣的可以找我交流一下
  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?