Jump to content


Photo

remove www from url


  • Please log in to reply
31 replies to this topic

#21 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 07 September 2006 - 12:38 PM

http://mysite.com returns http://mysite.com not mysite.com

#22 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 September 2006 - 12:44 PM

How are you using the code within your PHP script? and what version of PHP are you running.

#23 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 07 September 2006 - 12:48 PM

Here's the output I get, agrees with brown2005...

www.somesite.com -> somesite.com
http://www.somesite.com -> somesite.com
http://somesite.com -> http://somesite.com

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#24 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 September 2006 - 12:49 PM

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:
$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);


I did it the long way, using (http:\/\/www.|http:\/\/|www\.), it can be shortened but I'm an new to expressions

#25 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 07 September 2006 - 12:57 PM

also i have seen websites with https://www.somesite.com , but these only print out https://www.somesite.com to....

#26 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 September 2006 - 01:15 PM

Try this:
<?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;

?>


#27 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 07 September 2006 - 01:18 PM

ok, here's a shorter version... might achieve what you're after.

<?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);
?>

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  ;)

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#28 Nicklas

Nicklas
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts
  • Locationwww.nswardh.com

Posted 07 September 2006 - 01:47 PM

Here´s one that takes care of ftp://, ftp., http://, https:// and www

$string = "Here´s a ftp://mysite.com - ftp.mysite.com string and https://mysite.com with a
link 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);


#29 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 07 September 2006 - 01:55 PM

Nicklas... why the parenthesis arount the first 's'?

You don't want to match it, and it's not being user for alternation.

Surely https? says match http and an optional s

Regards
Rich


Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#30 Nicklas

Nicklas
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts
  • Locationwww.nswardh.com

Posted 07 September 2006 - 02:40 PM

Yeah, I know, but for some reason, the expression falls apart if I do it that way, if I put the optional s in a subpattern, it works.
Anyways, here´s a small update, the previous regexp removed everything as it should, but left the www untouched. this has now been fixed.

$string = "Here's a ftp://mysite.com - ftp.mysite.com string and https://mysite.com with a
link 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);



#31 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 07 September 2006 - 03:28 PM

omg, i didnt know this would cause so much action.. lol.. i presume some of u want to use this yourselves...

#32 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 07 September 2006 - 03:29 PM

i will try some when i get back home as im out at the mo, thanks all for taking time to help...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users