How to extract substring by start-index and end-index?

后端 未结 5 1218
花落未央
花落未央 2021-02-02 06:12
$str = \'HelloWorld\';
$sub = substr($str, 3, 5);
echo $sub; // prints \"loWor\"

I know that substr() takes the first parameter, 2nd parameter is start

5条回答
  •  闹比i
    闹比i (楼主)
    2021-02-02 06:41

    It's just math

    $sub = substr($str, 3, 5 - 3);
    

    The length is the end minus the start.

提交回复
热议问题