Forum Moderators: coopster

Message Too Old, No Replies

Regex to remove "http://" and similar.

         

whoisgregg

11:40 pm on Jul 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Trying to accept any form of url and remove the protocol bit at the beginning...

This is doing a good job with http and https:

preg_replace('¦https?:[/]+¦i', '', $url_to_check);

This is returning an empty string:

preg_replace('¦(http¦ftp)s?:[/]+¦i', '', $url_to_check);

How do I fork the regex to test for different possibilities? Is there a better way to do this?

Oddly, google tells me I searched for "preg_replace remove http" last October, but I can't remember how I solved it then. :/

whoisgregg

11:51 pm on Jul 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Figured it out:

preg_replace('/^(http¦ftp¦news)s?:\/+/i', '', $url_to_check)

And don't even ask me how those pipes crept in above instead of forward slashes... ::shakes head::