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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

broken regex for hostname from url

Status
Not open for further replies.

thedaver

IS-IT--Management
Joined
Jul 12, 2001
Messages
2,741
Location
US
I want to find the true hostname from the "host" returned by parse_url. In Unix / Linux / TCP/IP, a host's name is " from the hostname "
I had the regex working, but I also needed to filter out any bozos who would erroenously enter "try.to.break.the.url. Again, I want to match/select the " hostname and can do without the rest. I thought that this code would work, but it doesn't.

Maybe some fresh eyes will spot my error. Thanks.

<?php

function real_host ($host=&quot;&quot;) {
if (! $host) {return (&quot;&quot;);}

// This regex pair is belt-and-suspenders redundancy from
// my debug efforts. The first one should be sufficient
// to filter out bogus prefix's on the hostname, but it
// doesn't get the job done.....

if (preg_match(&quot;/^.*?\.?(.*)\.(.*)\.(.*)$/&quot;,$host,$matches))
{ if (preg_match(&quot;/^.*?\.?(.*)$/&quot;,$matches[1],$final))
{return $final[1];}
}
return (&quot;&quot;);
}

$arr=&quot;&quot;;

// My objective is to return &quot; from function real_host

$arr=parse_url(strtolower(&quot;print_r($arr);
print &quot;<br>\n&quot;;
print real_host($arr[&quot;host&quot;]);
?>

Surfinbox.com Business Internet Services - National Dialup, DSL, T-1 and more.
 
You have some assumptions wrong.

&quot; is not the FQDN of a machine. &quot; is.

Although &quot; is a FQDN, it's also possible that &quot;foo.com&quot; can be, too. DNS will allow you to relate an IP address to &quot;foo.com&quot; for the purposes of IP resolution. What will your code do if the string &quot; does not appear in the input URL?

It's also possible that &quot;foo. is the correct FQDN of a machine.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top