PHP tricks and tips

How to recursive left trim a string in PHP

PHP have functions to trim from the beginning (ltrim) of a string or from the end of a string, or both form beginning and end.

When you call

echo ltrim('..mystring');

you get


But how you can remove all these continuous characters from the beginning of a string?

You can write a short function, because PHP hasn’t built-in function for this purpose.

function r_ltrim($string, $char = '' )
	if ( '' == $char ) {
		$string = ltrim($string);
	} else {
		while ($char == substr($string, 0, 1)) {
			$string = ltrim($string, $char);
	return $string;