MirageSF
asked on
Stripping http://www. from a domain name stored in a variable
Hi,
I have a variable called $domain, that should contain http://www.domainname.com etc, but I need to strip out the http://www. etc to leave me with domainname.com,
but it also needs to be fairly flexible as sometimes the variable may only contain www.domainname.com etc, or could even already store domainname.com or even have
spelling errors in the beginning as its human entered, i.e. htp:/ww.domainname.com etc, but I still need to strip out that domainname.com so the $domain variable equals that regardless. Also some domains come as domainname.co.uk so will need to take account of this incase it strips the wrong stuff out !
Regards
I have a variable called $domain, that should contain http://www.domainname.com etc, but I need to strip out the http://www. etc to leave me with domainname.com,
but it also needs to be fairly flexible as sometimes the variable may only contain www.domainname.com etc, or could even already store domainname.com or even have
spelling errors in the beginning as its human entered, i.e. htp:/ww.domainname.com etc, but I still need to strip out that domainname.com so the $domain variable equals that regardless. Also some domains come as domainname.co.uk so will need to take account of this incase it strips the wrong stuff out !
Regards
sorry writing code again
<?
$domain="http://www.numen.biz";
$tmp=explode('.',$domain);
$cnt=count($tmp);
$newdomain="{$tmp[$cnt-2]} .{$tmp[$cn t-1]}";
echo $newdomain;
?>
<?
$domain="http://www.numen.biz";
$tmp=explode('.',$domain);
$cnt=count($tmp);
$newdomain="{$tmp[$cnt-2]}
echo $newdomain;
?>
hope this answer to be accepted ha ha
ASKER
That seems to overdo it a little, like test.co.uk becomes ukco lol
ASKER
ok, second one works better but it stuggles with domainname.co.uk producing co.uk ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanx that seems to work perfect :)
hi let me tell you one thing you can never do this perfectly ok
what you can do id create some probabilities...still you may skip some other case..
ok try this...
<?
$domain="http://www.numen.co.uk";
$tmp=explode('.',$domain);
$tmp=array_slice($tmp,1);
$newdomain=implode('.',$tm p);
echo $newdomain;
?>
what you can do id create some probabilities...still you may skip some other case..
ok try this...
<?
$domain="http://www.numen.co.uk";
$tmp=explode('.',$domain);
$tmp=array_slice($tmp,1);
$newdomain=implode('.',$tm
echo $newdomain;
?>
thats bad
really bad
very late
really bad
very late
ASKER
Sorry Carchitect, your original supply was so blimin' fast I nearly fell off my chair ! Never seen anything like it, unfortunately _spookje beat you to the post on what I needed working, and ive tested it quite alot and seems to work great :)
Best Regards
Best Regards
Actually I can improve my expression, adding a questionmark:
$newdomain = preg_replace('/.*?ww\./', '', $domain);
Like this everything will be thrown away until the FIRST
occurance of 'ww.'.
So it won't work for domains without www: for example
images.google.com.
If you say the first part of the domain (everything before
the first point) must be deleted, then you can better use:
$newdomain = preg_replace('/.*?\./', '', $domain);
Sorry for any inconvinience caused ...
$newdomain = preg_replace('/.*?ww\./', '', $domain);
Like this everything will be thrown away until the FIRST
occurance of 'ww.'.
So it won't work for domains without www: for example
images.google.com.
If you say the first part of the domain (everything before
the first point) must be deleted, then you can better use:
$newdomain = preg_replace('/.*?\./', '', $domain);
Sorry for any inconvinience caused ...
ASKER
oh dear, didnt test it that far lol, that latest one strips everything ! testdomain.co.uk becomes uk
Gee, you are right! :-) if you replace the $newdomain
line with the following, it is fixed though:
preg_match('/.*?\.(.*)/', $domain, $tmp);
$newdomain = $tmp[1];
It is in fact the same perl expression, but maybe
preg_replace is confused by the '?' ?
What about domains like:
http://google.com
Does that have to work too? Then you will really have
a problem BTW :-)
line with the following, it is fixed though:
preg_match('/.*?\.(.*)/', $domain, $tmp);
$newdomain = $tmp[1];
It is in fact the same perl expression, but maybe
preg_replace is confused by the '?' ?
What about domains like:
http://google.com
Does that have to work too? Then you will really have
a problem BTW :-)
ASKER
yup, everything before the actual domain name needs stripping, http://www.domainnamehere.co.uk, http://www. needs to go, or anything upto the actual domain name anyway, once it hits the domain name it can stop deleting stuff
It's hard to program 'what is the actual domainname'...
You will have to make a choice I think:
Strip everything until the first occurance of 'ww.':
$newdomain = preg_replace('/^.*?ww\./', '', $domain);
Strip everthing until the first dot:
$newdomain = preg_replace('/^.*?\./', '', $domain);
This will convert images.google.com into google.com,
but will also convert google.com into com!
If you know that always you have ww in the string, you
can better take the first one I think.
BTW I looked it up, the problem with '?' is, that PHP
wants a '^' in the beginning of the regexp.
I hope you can make a good choice now, and one of the
suggestions works well enough. Otherwise please post
a list of URL and their domains, then I will try to
find a better regexp for you :-).
You will have to make a choice I think:
Strip everything until the first occurance of 'ww.':
$newdomain = preg_replace('/^.*?ww\./',
Strip everthing until the first dot:
$newdomain = preg_replace('/^.*?\./', '', $domain);
This will convert images.google.com into google.com,
but will also convert google.com into com!
If you know that always you have ww in the string, you
can better take the first one I think.
BTW I looked it up, the problem with '?' is, that PHP
wants a '^' in the beginning of the regexp.
I hope you can make a good choice now, and one of the
suggestions works well enough. Otherwise please post
a list of URL and their domains, then I will try to
find a better regexp for you :-).
i told in my answer
hi let me tell you one thing you can never do this perfectly ok
what you can do id create some probabilities...still you may skip some other case..
so its better to make some case statement sort of thing and don't try only one solution try few and implement in code...
like domain names can be
http://numen.biz
http://images.google.com
http://www.hotmail.com etc
so many variations so if yo want to get it work for every choice then you really have to write a nice code...and pls post your final answer here.
hi let me tell you one thing you can never do this perfectly ok
what you can do id create some probabilities...still you may skip some other case..
so its better to make some case statement sort of thing and don't try only one solution try few and implement in code...
like domain names can be
http://numen.biz
http://images.google.com
http://www.hotmail.com etc
so many variations so if yo want to get it work for every choice then you really have to write a nice code...and pls post your final answer here.
<?
$tmp=explode('.',$domain);
$cnt=count($tmp);
now create new variable
$newdomain=$tmp[$cnt-1].$t
echo $newdomain;
?>