GGshow reloaded GGshow reloaded

December 11, 2012

PHP regular expression to strip/trim protocol from the beginning of an URL.

To remove http://, https://, ftp://, sftp://, www

echo preg_replace('#^[^:/.]*[:/]+(www.)?#i','',$url);

Examples:

function strip_url($url){
  return return preg_replace('#^[^:/.]*[:/]+(www.)?#i','',$url);
}
echo strip_url('http://www.ggshow.com');
echo strip_url('https://www.ggshow.com');
echo strip_url('ftp://www.ggshow.com');
echo strip_url('sftp://www.ggshow.com');
echo strip_url('http://ggshow.com');
echo strip_url('http:/www.ggshow.com');
echo strip_url('http:www.ggshow.com');
echo strip_url('http:ggshow.com');
echo strip_url('HTTP://WWW.ggshow.com');
echo strip_url('anything://www.ggshow.com');
echo strip_url('anything//www.ggshow.com');
echo strip_url('//www.ggshow.com');
echo strip_url('/www.ggshow.com');

Every echo statement above will output ggshow.com

Filed under: PHP — Tags: , , , , , , , , , , — GG @ 8:56 pm

No Comments »

No comments yet.

RSS feed for comments on this post.

Leave a comment

© 2017 GGSHOW | Powered by WordPress