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;
}
3
  • It would help if you could describe what the function does in more detail. Commented Mar 14, 2014 at 21:38
  • It extract the name of the website... Here is what the function does: ghostbin.com/paste/7cs6p Commented Mar 14, 2014 at 21:55
  • Here is where i found the PHP code and it's exactly what i'm trying to recreate in objective-c: stackoverflow.com/questions/12689183/… Commented Mar 14, 2014 at 22:00

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.