extractdomainname

22 views
Skip to first unread message

Brian Armknecht

unread,
Mar 12, 2013, 10:57:09 PM3/12/13
to yub...@googlegroups.com
Hi Jonathan,

The "extractdomainname" command, which is currently hosted at jonathanaquino.com, is no longer working:

If it needs to be hosted elsewhere or rewritten, please let me know.

It's required for the ">" command, which I miss!  http://yubnub.org/kernel/man?args=%3E

Thanks,
Brian

Jonathan Aquino

unread,
Mar 19, 2013, 1:01:56 PM3/19/13
to YubNub
Hi Brian,

It'd be great to have the code in PHP - could you try your hand at translating it? The Ruby code is:

<%
def extractDomainName(url)
    r = url=~(/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/) ? $1 : 'Not a valid URL!'
    r.gsub!(/((?:www)|(?:seek)|(?:query)|(?:search))\.(([^\.]+)\.([^\.]+)(\.([^\.]+))?)/, '\2')
    r.gsub!(/\:\d+$/, '')
    r
end

require "cgi"
cgi = CGI.new
puts extractDomainName(cgi["url"])
%>

I tried converting it to PHP but it wasn't passing all of the tests shown on the man page, so it needs some more work:

$url = preg_replace('/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/', '\1', $url);
$url = preg_replace('/((?:www)|(?:seek)|(?:query)|(?:search))\.(([^\.]+)\.([^\.]+)(\.([^\.]+))?)/', '\2', $url);
$url = preg_replace('/\:\d+$/', '', $url);
echo $url;

Jonathan


--
You received this message because you are subscribed to the Google Groups "YubNub" group.
To unsubscribe from this group and stop receiving emails from it, send an email to yubnub+un...@googlegroups.com.
To post to this group, send email to yub...@googlegroups.com.
Visit this group at http://groups.google.com/group/yubnub?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

fpm

unread,
Mar 19, 2013, 1:51:35 PM3/19/13
to yub...@googlegroups.com
How about using the power of php and the built in parse_url function, rather than the complex regular expression?
http://www.php.net/manual/en/function.parse-url.php

$urls   = array();
$urls["http://www.amazon.com"]                                              = "amazon.com";
$urls["eemadges.com"]                                                           = "eemadges.com";
$urls["http://en.wikipedia.org?serarch=%s"]                              = "en.wikipedia.org";
$urls["http://seek.sing365.com:8080/cgi-bin/s.cgi?q=ladytron"]   = "sing365.com";
$urls["https://www.cia.gov/cia/publications/factbook/geos/.html"] = "cia.gov";

foreach ( $urls as $url => $result ) {
  $url = preg_match(";^(http://|https://|ftp://|file://);","$url") ? $url : "http://$url";
  $parts = parse_url($url);
  $domainname = $parts['host'] ? preg_replace(';^(www|seek|query|search)\.;','',$parts['host']) : 'Not a valid URL!';

  if ( $domainname != $result ) echo("NOT MATCHING SPECS\n");

  printf("domainname=%s\n",$domainname);
}



Jonathan Aquino

unread,
Mar 20, 2013, 9:52:33 AM3/20/13
to YubNub
Thanks fpm - I fixed up extractDomainName using your code.

Jonathan



}



Brian Armknecht

unread,
Mar 21, 2013, 10:24:22 AM3/21/13
to yub...@googlegroups.com
Thanks, Jonathan and fpm!
Reply all
Reply to author
Forward
0 new messages