Remove bit of a string

Hey,

I haver this script:

		$i = -1;
		while (substr($docRoot, $i, 1) != '/') {
			$docRoot = substr_replace($docRoot, '', -1);
			$i--;
		}

I am running it on this string as $docRoot:

/home/test/public_html/lymmog

I am expecting it to come out like:

/home/rctneil/public_html/

but it comes out as:

/home/rctneil/public_html/lym

Any ideas why it’s only doing half?

You don’t need to use $i–

Your string length changes with each loop so by using $i-- you’re screwing things up.

I ran the following through writecodeonline.com/php and it works fine:


$docRoot = '/home/test/public_html/lymmog';
        while (substr($docRoot, -1, 1) != '/') {
            $docRoot = substr_replace($docRoot, '', -1);
        }
print $docRoot;