-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,672
  • 2
  • 17
  • 41

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,663
  • 5
  • 28
  • 40