substr() return incorrect number of characters

前端 未结 2 1461
清歌不尽
清歌不尽 2021-01-26 11:28

substr() is returning 28 characters instead of 25.

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

相关标签:
2条回答
  • 2021-01-26 11:39

    In substr() function third parameter is used to set the length not to set the position. For more details please visit

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

    0 讨论(0)
  • 2021-01-26 11:57

    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

    0 讨论(0)
提交回复
热议问题