Checking Text Contain Domain/URL or Not

Sometimes programmers/developers need to check text contains the domain/URL or not. Here is the code snippet for checking domain/URL contain or not in the text.


 * This function will check the text and find the domain name from test.
 * If any domain found then it will validate domain name. If domain valid
 * then return true otherwise return false.
 * @param $textString
 * @return bool
function isDomainContain($textString)
    $pattern = '/(http[s]?\:\/\/)?(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}/';
    preg_match($pattern, $textString, $domains);
    foreach ($domains as $domain){
        if (filter_var($domain, FILTER_VALIDATE_URL) || checkdnsrr($domain)) {
            return true;

    return false;

$textString = 'Stack Overflow is a question and answer site for professional and enthusiast programmers. 
It is a privately held website, the flagship site of the Stack Exchange Network, created in 2008 by Jeff 
Atwood and Joel Spolsky. It features questions and answers on a wide range of topics in computer programming.';

    echo 'Domain found';
} else {
    echo 'Domain not found';