PDA

View Full Version : regex for trailing slashes?


leonardburton
12-03-2004, 02:46 AM
Could someone please help me come up with a regex to determine if a trailing slash needs to be added to a url?

for instance http://www.whatever.com needs to become http://www.whatever.com/

and http://www.whatever.com/foo needs to be http://www.whatever.com/foo/

but http://www.whatever.com/foo.php needs to remain the same

Thanks,

Leonard

Charter
12-03-2004, 04:06 AM
// assumes all filenames are of name.ext format
$url = "http://www.domain.com";
$part = pathinfo($url,PATHINFO_EXTENSION);
$last = substr($url, -1);
if (($last != "/") && (empty($part['extension']))) {
$url = $url."/";
}
echo $url;