How can the issue of detecting all links in a text, regardless of their placement, be addressed using PHP?

Issue: To detect all links in a text, regardless of their placement, we can use regular expressions in PHP to search for URLs within the text and extract them. PHP Code:

$text = "This is a sample text with a link https://www.example.com and another link www.google.com";

// Regular expression to find URLs in the text
$pattern = '/(https?:\/\/)?(www\.)?[\w\.-]+\.[a-z]{2,}(\.[a-z]{2,})?/';

// Find all URLs in the text
preg_match_all($pattern, $text, $matches);

// Output all the detected links
foreach ($matches[0] as $link) {
    echo $link . "<br>";
}