Sub-string without counting blank spaces

后端 未结 3 2038
抹茶落季
抹茶落季 2020-12-21 23:07

I want to make a sub-string, where the $count only counts letters, not spaces. This is what I have so far:

$string =\"vikas tyagi php\";
$strin         


        
3条回答
  •  悲哀的现实
    2020-12-21 23:47

    Simply count the spaces and add them to the desired length of the capture:

    function spaceless_substr($string, $start, $count) {
        return substr($string, $start, ($count+substr_count($string, ' ', $start, $count)));
    }
    
    $string ="vikas tyagi asd sd as asd";
    echo substr($string, 0, 14);
    // return: "vikas tyagi a"
    echo spaceless_substr($string, 0, 14);
    // return: "vikas tyagi asd" 
    

提交回复
热议问题