-1

I would like to search through the strings I have and pull out the domain name that may end with .com .net etc...

$string = "Just head over to nba.com";
$string = "visit cnn.net"; 

Please help me extract the domain name above into a variable.

Thank you in advance.

3
  • 1
    What have you tried and what issues did you encounter with those solutions? Commented Jul 29, 2013 at 6:09
  • Is domain always the last item on the string or it may appear at different positions ? Commented Jul 29, 2013 at 6:10
  • Googling php parse url from string turns up many useful solutions. Commented Jul 29, 2013 at 6:10

1 Answer 1

1
if (preg_match_all('/\w+\.(com|net)/', $text, $matches)){
   print_r($matches);
}

The first match will be:

echo $matches[0][0];
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.