?
Solved

Stripping http://www. from a domain name stored in a variable

Posted on 2003-03-13
16
Medium Priority
?
265 Views
Last Modified: 2006-11-17
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
Comment
Question by:MirageSF
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 4
16 Comments
 
LVL 6

Expert Comment

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

now create new variable
$newdomain=$tmp[$cnt-1].$tmp[$cnt-2];
echo $newdomain;
?>
0
 
LVL 6

Expert Comment

by:carchitect
ID: 8129036
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
 
LVL 6

Expert Comment

by:carchitect
ID: 8129057
hope this answer to be accepted ha ha
0
Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

 

Author Comment

by:MirageSF
ID: 8129084
That seems to overdo it a little, like test.co.uk becomes ukco lol
0
 

Author Comment

by:MirageSF
ID: 8129102
ok, second one works better but it stuggles with domainname.co.uk producing co.uk ?
0
 
LVL 1

Accepted Solution

by:
spookje earned 500 total points
ID: 8129652
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
 

Author Comment

by:MirageSF
ID: 8130038
Thanx that seems to work perfect :)
0
 
LVL 6

Expert Comment

by:carchitect
ID: 8130105
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
 
LVL 6

Expert Comment

by:carchitect
ID: 8130126
thats bad
really bad
very late
0
 

Author Comment

by:MirageSF
ID: 8130148
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
 
LVL 1

Expert Comment

by:spookje
ID: 8130333
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
 

Author Comment

by:MirageSF
ID: 8130385
oh dear, didnt test it that far lol, that latest one strips everything !  testdomain.co.uk becomes uk
0
 
LVL 1

Expert Comment

by:spookje
ID: 8130491
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
 

Author Comment

by:MirageSF
ID: 8130581
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
 
LVL 1

Expert Comment

by:spookje
ID: 8130982
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
 
LVL 6

Expert Comment

by:carchitect
ID: 8134107
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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question