Jump to content

need to trim the string


piyush23424

Recommended Posts

$domain = $_SERVER['HTTP_HOST'];
$path = $_SERVER['REQUEST_URI'];
if($domain != 'site.com') { header("Location: http://site.com".$path.""); }

 

I am not using this to redirect but there is a functionality in my site when i put www with domain name then it works and it doesn't work when i put the url like http://sitename.com

If you are wanting to modify a URL that is not related to the URL the user is currently viewing (i.e. user entered value), then this will work:

 

preg_replace("/www\./", '', $url);

 

It has one flaw. If the url contains 'www.' anywhere in the url it will be removed. Ex: "subdomainwww.domain.com" would become "subdomaindomain.com". I did that so it would work for values such as "http://www.domain.com". But, I think the likelyhood of such an occurance would be very remote.

 

However, if the values will always start from the subdomain (i.e. not 'http://') then this would be better:

preg_replace("/^www\./", '', $url);

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.