views:

206

answers:

4

I'm so bad at regexp, but I'm trying to get some/path/image.jpg out of http://somepage.com/some/...etc and trying this method:

function removeDomain($string) {

    return preg_replace("/http:\/\/.*\//", "", $string);

}

It isn't working -- so far as I can tell it's just returning a blank string. How do I write this regexp?

+4  A: 

you should use parse_url

SilentGhost
perfect, thank you
Carson Myers
+2  A: 

Hi,

you might want to use this rather than regex: http://cz2.php.net/manual/en/function.parse-url.php this will break up the URL for you, so you just read the resulting array for the domain name

meosoft
A: 

Use parse_url as other people have already said.

But to answer your question about why your regex isn't working, it will match an entire URL because .* matches anything, and indeed it is. It is matching the whole URL, and replacing it with an empty string, hence your results. Try the following instead which will only match a hostname (anything up to the first '/'):

function removeDomain($string) {
    return preg_replace("@^https?://[^/]+/@", "", $string);
}
Matthew Scharley
A: 

While SilentGhost is right, the reason your regex is failing is because .* is greedy, and will eat everything, as long as there is a / afterwards.

If you put a ? mark after your .*, it will only match until the first /

function removeDomain($string) {

    return preg_replace("/http:\/\/.*?\//", "", $string);

}
ABentSpoon