Jump to content

Archived

This topic is now archived and is closed to further replies.

brown2005

remove www from url

Recommended Posts

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

Share this post


Link to post
Share on other sites
I agree with Andy. I gave it a shot, however there are some problems, maybe you or someone can expound on it:

[code]
<?php

function 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]
<?php

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

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

?>
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ....

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
[code=php:0]
$websites_website =  StripUrl($homepages_array['websites_website']);
[/code]

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
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]

Regards
Rich

Share this post


Link to post
Share on other sites
Try something like this. This regexp removes the HTTP:// and the WWW. if it´s present in the link

ex:
[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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
$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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
How are you using the code within your PHP script? and what version of PHP are you running.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
also i have seen websites with https://www.somesite.com , but these only print out https://www.somesite.com to....

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.