Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

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
0
MirageSF
Asked:
MirageSF
  • 6
  • 6
  • 4
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now