Skip to content

Instantly share code, notes, and snippets.

@sohelrana820
Created March 12, 2021 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sohelrana820/e973a02bbb1ff1054788f4f59e35342e to your computer and use it in GitHub Desktop.
Save sohelrana820/e973a02bbb1ff1054788f4f59e35342e to your computer and use it in GitHub Desktop.
Check text string contain domain ot not.
<?php
/**
* 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.
https://stackoverflow.com/users/2272167/sohel-rana';
if(isDomainContain($textString)){
echo 'Domain found';
} else {
echo 'Domain not found';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment