I would use something like this: if(substr($string, 0, strlen($_SERVER['DOCUMENT_ROOT'])) == $_SERVER['DOCUMENT_ROOT']) $string = substr($string, strlen($_SERVER['DOCUMENT_ROOT'])); That says if $string starts with the doc root, drop the beginning characters for the length of the doc root.