wildteen88 Posted September 7, 2006 Share Posted September 7, 2006 Try this:[code=php:0]<?php$string = "Here´s a ftp://mysite.com string https://mysite.com with a link to www.somesite.com bla bla";$string2 = preg_replace('/([a-z]+:\/\/|[a-z]+:\/\/?www.)([-a-z0-9_][-a-z0-9_\.]+\.[a-z]{2,4}(\/[-a-z0-9_\.%&+\/=&]+)?)/is', '\\2', $string);// remove any left over instances of www.$string2 = str_replace('www.', '', $string2);echo $string2;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/page/2/#findComment-87719 Share on other sites More sharing options...
HuggieBear Posted September 7, 2006 Share Posted September 7, 2006 ok, here's a shorter version... might achieve what you're after.[code=php:0]<?php$string = "Here´s a string https://mysite.com with a link to www.somesite.com bla bla";echo preg_replace('/(https?:\/\/)?(www\.)?([^\s]+)/', '\\3', $string);?>[/code]Also, there's another problem with your code wildteen88.. what about if my domain's http://www.ilovethewww.com ?What's going to happen then ;)RegardsRich Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/page/2/#findComment-87720 Share on other sites More sharing options...
Nicklas Posted September 7, 2006 Share Posted September 7, 2006 Here´s one that takes care of [color=blue]ftp://[/color], [color=blue]ftp.[/color], [color=blue]http://[/color], [color=blue]https://[/color] and [color=blue]www[/color][code=php:0]$string = "Here´s a ftp://mysite.com - ftp.mysite.com string and https://mysite.com with alink to www.somesite.com and to http://www.anothersite.com bla bla.Yet another link to http://mysite.com/somedir/mysite.html";echo preg_replace('/(http(s)?:\/\/|ftp(:\/\/|\.))(www\.)?(.*?[^\s])/is', '\\5', $string);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/page/2/#findComment-87735 Share on other sites More sharing options...
HuggieBear Posted September 7, 2006 Share Posted September 7, 2006 Nicklas... why the parenthesis arount the first 's'?You don't want to match it, and it's not being user for alternation.Surely [color=green][tt]https?[/tt][/color] says match [color=red][tt]http[/tt][/color] and an optional [color=red][tt]s[/tt][/color]RegardsRich Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/page/2/#findComment-87739 Share on other sites More sharing options...
Nicklas Posted September 7, 2006 Share Posted September 7, 2006 Yeah, I know, but for some reason, the expression falls apart if I do it that way, if I put the optional [b]s[/b] in a subpattern, it works.Anyways, here´s a small update, the previous regexp removed everything as it should, but left the [b]www[/b] untouched. this has now been fixed.[code=php:0]$string = "Here's a ftp://mysite.com - ftp.mysite.com string and https://mysite.com with alink to www.somesite.com and to http://www.anothersite.com bla bla.Yet another link to http://mysite.com/somedir/mysite.html";echo preg_replace('/((http(s)?:\/\/|ftp(:\/\/|\.)))?(www\.)?/is', '', $string);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/page/2/#findComment-87778 Share on other sites More sharing options...
brown2005 Posted September 7, 2006 Author Share Posted September 7, 2006 omg, i didnt know this would cause so much action.. lol.. i presume some of u want to use this yourselves... Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/page/2/#findComment-87813 Share on other sites More sharing options...
brown2005 Posted September 7, 2006 Author Share Posted September 7, 2006 i will try some when i get back home as im out at the mo, thanks all for taking time to help... Quote Link to comment https://forums.phpfreaks.com/topic/19871-remove-www-from-url/page/2/#findComment-87815 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.