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

后端 未结 5 1219
花落未央
花落未央 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:59

    function my_substr_function($str, $start, $end)
    {
      return substr($str, $start, $end - $start);
    }
    

    If you need to have it multibyte safe (i.e. for chinese characters, ...) use the mb_substr function:

    function my_substr_function($str, $start, $end)
    {
      return mb_substr($str, $start, $end - $start);
    }
    

提交回复
热议问题