How can URLs be extracted from a text and stored in an array in PHP?

To extract URLs from a text and store them in an array in PHP, you can use regular expressions to match the URLs in the text and then store them in an array. This can be useful for tasks like parsing text for links or extracting specific information from a text body.

<?php
$text = "Check out my website at https://www.example.com. Also, visit our blog at http://blog.example.com.";

$pattern = '/(https?:\/\/[^\s]+)/';
preg_match_all($pattern, $text, $matches);

$urls = $matches[0];

print_r($urls);
?>