Jump to content

remove www from url


brown2005

Recommended Posts

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]
Link to comment
https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-86950
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.
Link to comment
https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-86978
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]
Link to comment
https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87001
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....
Link to comment
https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87031
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]
Link to comment
https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87218
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
Link to comment
https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87677
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
Link to comment
https://forums.phpfreaks.com/topic/19871-remove-www-from-url/#findComment-87703
Share on other sites

Archived

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

×
×
  • Create New...

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.