Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Check text string contain domain ot not.
* 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';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment