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

后端 未结 5 1216
花落未央
花落未央 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条回答
  •  再見小時候
    2021-02-02 06:35

    Just subtract the start index from the end index and you have the length the function wants.

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

提交回复
热议问题