I need 2 functions that take a string and the number of chars to trim from the right and left side and return it. E.g:
$str = "[test]";
$str = ltrim($str,1); //becomes test]
$str = rtrim($str,1); //becomes test
Thoughts?
I need 2 functions that take a string and the number of chars to trim from the right and left side and return it. E.g:
$str = "[test]";
$str = ltrim($str,1); //becomes test]
$str = rtrim($str,1); //becomes test
Thoughts?
function my_rtrim($str, $count) {
return substr($str, 0, -$count);
}
function my_ltrim($str, $count) {
return substr($str, $count);
}
See substr().
substr("abcdef", 0, -1); // 'abcde'
substr("abcdef", 1); // 'bcdef'
but that's not how ltrim
, rtrim
functions usually work. They operate with substrings and not indexes.
and obviously:
substr('abcdef', 1, -1)
does what you want in one go.
I believe substr() can do the job. Both the $start and $length arguments can be negative.