0

i found this cool PHP function to extract the URL name from a url string. Now i would like to recreate the same function in Objective-C code. I'm still trying but without success... Can someone help me recreate this function?

Here is the PHP code:

function __extractName($url)
{
  $domain = parse_url($url , PHP_URL_HOST);
  if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $list)) {
    return substr($list['domain'], 0,strpos($list['domain'], "."));
  }
  return false;
}
mtshare
  • 3
  • 1
  • 3
  • It would help if you could describe what the function does in more detail. – Tom Harrington Mar 14 '14 at 21:38
  • It extract the name of the website... Here is what the function does: https://ghostbin.com/paste/7cs6p – mtshare Mar 14 '14 at 21:55
  • Here is where i found the PHP code and it's exactly what i'm trying to recreate in objective-c: http://stackoverflow.com/questions/12689183/how-to-get-url-host-using-php – mtshare Mar 14 '14 at 22:00

1 Answers1

0

Why would you even bother with this?

NSURL *exampleURL = [NSURL URLWithString:@"<.....>"];
NSString *host = [exampleURL host];
if (host) NSLog(@"host is %@", host);

That will return the hostname. If you need to do further processing, e.g. return "google" for both "google.com" and "google.co.uk", then that is just some more simple string processing.

greymouser
  • 3,133
  • 19
  • 22