brown2005 Posted September 6, 2006 Share Posted September 6, 2006 i have an echo which echos saywww.hello.co.ukwhat i want to do is create a function that will strip the www. and just leavehello.co.ukis this possible.Thanks in advanceRegradsRichard Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/ Share on other sites More sharing options...
AndyB Posted September 6, 2006 Share Posted September 6, 2006 str_replace() looks like a candidate Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-86946 Share on other sites More sharing options...
hostfreak Posted September 6, 2006 Share Posted September 6, 2006 I agree with Andy. I gave it a shot, however there are some problems, maybe you or someone can expound on it:[code]<?phpfunction strip($domain) {$domain = str_replace("www.","",$domain);return $domain;}echo strip(www.domain.com);?>[/code]For some reason all the periods are not showing.After applying what wildteen88 said:[code]<?phpfunction strip($domain) {$domain = str_replace("www.","",$domain);return $domain;}echo strip('www.domain.com');?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-86950 Share on other sites More sharing options...
wildteen88 Posted September 6, 2006 Share Posted September 6, 2006 When calling the function it should be this:[code]echo strip('www.domain.com');[/code]Otherwise PHP will try to find the constants www, domain and com and join them to gether. When using a string it must be in quotes.Also I have modified your title, please try to use descriptive titles when posting threads. Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-86978 Share on other sites More sharing options...
brown2005 Posted September 6, 2006 Author Share Posted September 6, 2006 hi, the above code works fine.How can I add to this code to change anything put in front...i.e.http://www.http://www.etc, etc .... Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-86999 Share on other sites More sharing options...
wildteen88 Posted September 6, 2006 Share Posted September 6, 2006 Try this:[code=php:0]function strip($domain){ // get the segments of the url into an array // http://php.net/parse-url - for more info $url = parse_url($domain); $domain = str_replace("www.", "", $url['host']); return $domain;}echo strip('http://www.domain.com');[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87001 Share on other sites More sharing options...
brown2005 Posted September 6, 2006 Author Share Posted September 6, 2006 thanks very much for all your help Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87012 Share on other sites More sharing options...
brown2005 Posted September 6, 2006 Author Share Posted September 6, 2006 hi sorry me again..... i am using the above function.... but wat if i have while($homepages_array = mysql_fetch_array($homepages_query)) { $websites_id = $homepages_array[websites_id]; $websites_website = StripUrl('$homepages_array[websites_website]');echo" <tr> <td> </td> <td class='text' height='25'><a href='$config_website_url/$config_website_url_topic/files/url.php?id=$websites_id' class='red_bold_none'>$websites_website</a></td> <td class='text' height='25'> <a href='a' class='red_bold_none'>Delete</a> - <a href='' class='red_bold_none'>Edit</a> - <a href='' class='red_bold_none'>Stats</a> </td> </tr>";echo" <tr> <td colspan='3'><img src='$config_url/include/images/lines/blue.gif' width='100%' height='3'></td> </tr>"; }it doesnt print owt.... Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87031 Share on other sites More sharing options...
brown2005 Posted September 6, 2006 Author Share Posted September 6, 2006 can anyone help with the above please. need this to work. Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87093 Share on other sites More sharing options...
trq Posted September 6, 2006 Share Posted September 6, 2006 [code=php:0]$websites_website = StripUrl($homepages_array['websites_website']);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87095 Share on other sites More sharing options...
brown2005 Posted September 6, 2006 Author Share Posted September 6, 2006 nope still comes out blank Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87101 Share on other sites More sharing options...
brown2005 Posted September 6, 2006 Author Share Posted September 6, 2006 rite i know why it doesnt work.going on wildteen88's code, if someone put just domain.com and misses out both the http://www. then it echo's nothing, so how do i combat this please.... Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87143 Share on other sites More sharing options...
HuggieBear Posted September 6, 2006 Share Posted September 6, 2006 I'd have thought you were better off with a Regular Expression for this...Check out preg_match() in the [url=http://www.php.net/manual/en/function.preg-match.php]PHP Documentation[/url]RegardsRich Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87207 Share on other sites More sharing options...
Nicklas Posted September 6, 2006 Share Posted September 6, 2006 Try something like this. This regexp removes the HTTP:// and the WWW. if it´s present in the linkex:[code=php:0]$string = "Here´s a string with a link to www.somesite.com bla bla";echo preg_replace('/(http:\/\/)?www\.([-a-z0-9_][-a-z0-9_\.]+\.[a-z]{2,4}(\/[-a-z0-9_\.%&+\/=&]+)?)/is', '\\2', $string);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87218 Share on other sites More sharing options...
brown2005 Posted September 7, 2006 Author Share Posted September 7, 2006 I have now solved this myself. The above code does not work. Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87656 Share on other sites More sharing options...
brown2005 Posted September 7, 2006 Author Share Posted September 7, 2006 Wildteen, sorry for posting the item twice, but you state the code above would do what I want it to do, but it doesnt, but I have solved it now.Thanks for everyone's help. Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87657 Share on other sites More sharing options...
hostfreak Posted September 7, 2006 Share Posted September 7, 2006 brown2005, for my own knowledge, do you mind posting the code that you have made? Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87664 Share on other sites More sharing options...
wildteen88 Posted September 7, 2006 Share Posted September 7, 2006 It worked fine for me. Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87665 Share on other sites More sharing options...
brown2005 Posted September 7, 2006 Author Share Posted September 7, 2006 $string = "Here´s a string with a link to www.somesite.com bla bla";echo preg_replace('/(http:\/\/)?www\.([-a-z0-9_][-a-z0-9_\.]+\.[a-z]{2,4}(\/[-a-z0-9_\.%&+\/=&]+)?)/is', '\\2', $string);is that wat u are saying works fine? Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87673 Share on other sites More sharing options...
wildteen88 Posted September 7, 2006 Share Posted September 7, 2006 Yes. When I run that code, I get this:[quote]Here´s a string with a link to somesite.com bla bla[/quote]Notice www. has gone from somesite.com. it also works if you have this: [nobbc]http://www.mysite.com[/nobbc], it'll return mysite.com Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87677 Share on other sites More sharing options...
brown2005 Posted September 7, 2006 Author Share Posted September 7, 2006 http://mysite.com returns http://mysite.com not mysite.com Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87687 Share on other sites More sharing options...
wildteen88 Posted September 7, 2006 Share Posted September 7, 2006 How are you using the code within your PHP script? and what version of PHP are you running. Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87696 Share on other sites More sharing options...
HuggieBear Posted September 7, 2006 Share Posted September 7, 2006 Here's the output I get, agrees with brown2005...www.somesite.com -> somesite.comhttp://www.somesite.com -> somesite.comhttp://somesite.com -> http://somesite.comRegardsRich Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87702 Share on other sites More sharing options...
wildteen88 Posted September 7, 2006 Share Posted September 7, 2006 Are yes, Just noticed that it only works if you have www. after http://. All whats needed is a tweak in the expression to get it to work, which I am trying to work on now.try this:[code=php:0]$string = "Here´s a http://www.mysite.com string with a link to www.somesite.com bla bla";echo preg_replace('/(http:\/\/www.|http:\/\/|www\.)([-a-z0-9_][-a-z0-9_\.]+\.[a-z]{2,4}(\/[-a-z0-9_\.%&+\/=&]+)?)/is', '\\2', $string);[/code]I did it the long way, using (http:\/\/www.|http:\/\/|www\.), it can be shortened but I'm an new to expressions Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87703 Share on other sites More sharing options...
brown2005 Posted September 7, 2006 Author Share Posted September 7, 2006 also i have seen websites with https://www.somesite.com , but these only print out https://www.somesite.com to.... Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87709 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.