Fixing URL strings with reg expression?

Hey guys,

I’ve tried learning this stuff but I’m so bad at it. Was hoping you all could help me.

What would be the best way to fix a string that is a URL with …/ in it?

For example,
if a url is

http://www.example.com/foo/index.html

how would I fix it to be

http://www.example.com/foo/index.html

??

Thanks very much guys.

I’m confused as both links are exactly the same

Weird…
The first link is supposed to be:

http://www.example.com/foo/bar/".."/index.html

Looks like Sitepoint automatically reformats URLs that have relative path things in them, like “…/” which takes you to the previous directory. That’s why I have the quotes around it in the above code block.

Anyway, what Sitepoint does is exactly what I am trying to do! If it gets the above URL in this post, it strips the “…/” as well as the “bar/”

Found a nice regular expression online finally. I’m a bad searcher, sorry!

(ignore the double quotes in the url)


<?php 
$p = 'http://example.com/foo/bar/".."/index.html';

$p = preg_replace('/\\w+\\/\\.\\.\\//', '', $p);

echo $p; // outputs http://example.com/foo/index.html

?>