PDA

View Full Version : search and trim ?


fla5h
02-03-2003, 04:38 PM
I'm after a bit of help if someone can please lead me in the right direction.

I want a piece of PHP that will return me the host name of a url.

So

http://www.google.com/hello ..... = .... google.com
http://google.com/hello..... = .... google.com
www.google.com/hello..... = .... google.com

you get the picture ?

I have been trying to think of this logically ie

1. Search for 3rd '/' if it exists trim all to the right of it ( including '/' )
2. trim first 7 characters off the string.
3. Count the number of '.' in the string if more than one trim upto the first one.

So given

http://www.google.com/hello
step 1 = http://www.google.com
step 2 = www.google.com
step 3 = google.com

I cannot think of a better way ? Have I left a loop hole ?

If this follows through correctly, can someone let me know what I need to use in php to do this ?

Could do it in Javascript, but not php :(

Thanks for any guidance and help :)

fla5h
02-03-2003, 06:30 PM
mmm, this seems a better way. :)


<?php
//Get the web site address
preg_match("/^(http:\/\/)?([^\/]+)/i", "http://www.multi.part.domain.name.com/index.html", $matches);

$host = $matches[2];

// get all but one segments of host name
preg_match_all("/\.([^\.\/]+)/",$host,$matches);

// first element does not want the period('.')
$matches[0][0] = $matches[1][0];
$domain = implode('',$matches[0]);
echo( "Domain: " . $domain);
?>