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
MirageSFAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

carchitectCommented:
ok do one thing
<?
$tmp=explode('.',$domain);
$cnt=count($tmp);

now create new variable
$newdomain=$tmp[$cnt-1].$tmp[$cnt-2];
echo $newdomain;
?>
0
carchitectCommented:
sorry writing code again


<?
$domain="http://www.numen.biz";

$tmp=explode('.',$domain);
$cnt=count($tmp);

$newdomain="{$tmp[$cnt-2]}.{$tmp[$cnt-1]}";
echo $newdomain;
?>
0
carchitectCommented:
hope this answer to be accepted ha ha
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

MirageSFAuthor Commented:
That seems to overdo it a little, like test.co.uk becomes ukco lol
0
MirageSFAuthor Commented:
ok, second one works better but it stuggles with domainname.co.uk producing co.uk ?
0
spookjeCommented:
Why not with one expression?

It throws everything away until 'ww.' ... so it will not
work well for:

http://www.www.com => com :-)


<?
$domain = 'httpL://ww.domainname.co.uk';

$newdomain = preg_replace('/.*ww\./', '', $domain);

echo "Old domain $domain becomes $newdomain";
?>

_spookje
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MirageSFAuthor Commented:
Thanx that seems to work perfect :)
0
carchitectCommented:
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('.',$tmp);
echo $newdomain;
?>
0
carchitectCommented:
thats bad
really bad
very late
0
MirageSFAuthor Commented:
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
0
spookjeCommented:
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 ...
0
MirageSFAuthor Commented:
oh dear, didnt test it that far lol, that latest one strips everything !  testdomain.co.uk becomes uk
0
spookjeCommented:
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 :-)
0
MirageSFAuthor Commented:
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
0
spookjeCommented:
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 :-).
0
carchitectCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.