Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem:i want to print domain name from a url

Status
Not open for further replies.

perlkeats

Programmer
Jul 12, 2005
19
IN
hi

i have a list of urls stored in the database

i need to print only the domain names from those urls

to put simple

url:
i need to display only yahoo

can anyone help me in this.

keats
 
maybe that's hard to find, if you don't know about regular expressions: that's what i found on php.net:

<?php
// get Hostname
preg_match("/^(http:\/\/)?([^\/]+)/i",
" $treffer);
$host = $treffer[2];

// get last two segs from hostname
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $treffer);
echo "Domain-Name: {$treffer[0]}\n";
?>

so this should extract "yahoo.com", i'm sure you can change it to what you want...

__________________________________________
a programmer is a tool that converts coffee into code...
aschi666, sthcomputing.de
 
couldn't help it and had to find the correct regex, which just differed by one pair of brackets, so here's the solution:

<?php
// den Hostnamen aus URL holen
preg_match("/^(http:\/\/)?([^\/]+)/i",
" $treffer);
$host = $treffer[2];

// die letzten beiden Segmente aus Hostnamen holen
preg_match("/([^\.\/]+)\.[^\.\/]+$/", $host, $treffer);
echo "Der Domänen-Name lautet: {$treffer[1]}\n";
?>

enjoy!

__________________________________________
a programmer is a tool that converts coffee into code...
aschi666, sthcomputing.de
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top