Jump to content


Photo

remove www from url


  • Please log in to reply
31 replies to this topic

#1 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 06 September 2006 - 08:08 AM

i have an echo which echos say

www.hello.co.uk

what i want to do is create a function that will strip the www. and just leave

hello.co.uk

is this possible.

Thanks in advance

Regrads

Richard

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 06 September 2006 - 08:19 AM

str_replace() looks like a candidate
Legend has it that reading the manual never killed anyone.
My site

#3 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 06 September 2006 - 08:27 AM

I agree with Andy. I gave it a shot, however there are some problems, maybe you or someone can expound on it:

<?php

function strip($domain) {
$domain = str_replace("www.","",$domain);
return $domain;
}

echo strip(www.domain.com);

?>

For some reason all the periods are not showing.

After applying what wildteen88 said:
<?php

function strip($domain) {
$domain = str_replace("www.","",$domain);
return $domain;
}

echo strip('www.domain.com');

?>


#4 wildteen88

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

Posted 06 September 2006 - 10:05 AM

When calling the function it should be this:
echo strip('www.domain.com');

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.

#5 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 06 September 2006 - 11:00 AM

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 ....

#6 wildteen88

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

Posted 06 September 2006 - 11:06 AM

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


#7 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 06 September 2006 - 11:31 AM

thanks very much for all your help

#8 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 06 September 2006 - 12:06 PM

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>&nbsp;</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>&nbsp;&nbsp;-&nbsp;&nbsp;<a href='' class='red_bold_none'>Edit</a>&nbsp;&nbsp;-&nbsp;&nbsp;<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....

#9 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 06 September 2006 - 01:42 PM

can anyone help with the above please. need this to work.

#10 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 06 September 2006 - 01:44 PM

$websites_website =  StripUrl($homepages_array['websites_website']);


#11 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 06 September 2006 - 01:51 PM

nope still comes out blank

#12 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 06 September 2006 - 02:47 PM

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....

#13 HuggieBear

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

Posted 06 September 2006 - 04:08 PM

I'd have thought you were better off with a Regular Expression for this...

Check out preg_match() in the PHP Documentation

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

#14 Nicklas

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

Posted 06 September 2006 - 04:18 PM

Try something like this. This regexp removes the HTTP:// and the WWW. if it´s present in the link

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


#15 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 07 September 2006 - 11:32 AM

I have now solved this myself. The above code does not work.

#16 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 07 September 2006 - 11:33 AM

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.

#17 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 07 September 2006 - 11:47 AM

brown2005, for my own knowledge, do you mind posting the code that you have made?

#18 wildteen88

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

Posted 07 September 2006 - 12:00 PM

It worked fine for me.

#19 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 07 September 2006 - 12:25 PM

$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?

#20 wildteen88

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

Posted 07 September 2006 - 12:29 PM

Yes. When I run that code, I get this:

Here´s a string with a link to somesite.com bla bla

Notice www. has gone from somesite.com. it also works if you have this: http://www.mysite.com, it'll return mysite.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users