What are some best practices for handling URLs within a string parsing scenario in PHP?

When handling URLs within a string parsing scenario in PHP, it's important to properly identify and extract URLs from the string. One way to achieve this is by using regular expressions to match URL patterns within the string and extract them for further processing.

$string = "Check out this website: https://www.example.com for more information.";

// Match URLs using regular expression
preg_match_all('/(https?|ftp):\/\/[^\s\/$.?#].[^\s]*/', $string, $matches);

// Extract and print the matched URLs
foreach ($matches[0] as $url) {
    echo $url . "\n";
}