-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.

thevoipman
  • 1,773
  • 2
  • 17
  • 44

1 Answers1

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

The first match will be:

echo $matches[0][0];
Expedito
  • 7,771
  • 5
  • 30
  • 43