lesmith Posted January 28, 2009 Share Posted January 28, 2009 Hi chaps. I already have got a working preg_replace but I am sure it can be written alot better. It basically takes a url and strips http://www. Here is my code. $domain = "http://www.domain.com/"; $patterns[0] = '/\//'; $patterns[1] = '/http:/'; $patterns[2] = '/www./'; $replacements[0] = ''; $replacements[1] = ''; $replacements[2] = ''; echo preg_replace($patterns, $replacements, $domain); This works fine but I am wondering if it can be improved upon. Thank you if you can advice. Quote Link to comment Share on other sites More sharing options...
mattal999 Posted January 28, 2009 Share Posted January 28, 2009 $domain = "http://www.domain.com/"; $patterns = array('/\//', '/http:/', '/www./'); $replacements = array('', '', ''); echo preg_replace($patterns, $replacements, $domain); Hows that? Quote Link to comment Share on other sites More sharing options...
lesmith Posted January 28, 2009 Author Share Posted January 28, 2009 Thats cool. Thanks for that. Quote Link to comment Share on other sites More sharing options...
.josh Posted January 28, 2009 Share Posted January 28, 2009 There's no reason why all of that can't be combined: $domain = "http://www.domain.com/"; $pattern = "~http://www\.~"; $replacement = ""; echo preg_replace($pattern, $replacement, $domain); And actually, if that is the extent of your $domain string (the only thing in it will be the url), you can just use substr, as it is much faster: $domain = "http://www.domain.com/"; echo substr($domain, 11); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.