Here i will explain how to check the given string contains html tags or not. this function return boolean value.
// Function for check html tag presents
function hasTags($str){
$htmlTags = array('A' ,'B', 'BODY', 'BR', 'BUTTON', 'DIV', 'FONT', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'IMG', 'INPUT', 'TABLE');
// Here you mention what are the html tags you want to check
if(preg_match_all("~(<\/?)\b(".implode('|',$htmlTags).")\b([^>]*>)~i",$str,$matches)){
return TRUE;
}else{
return FALSE;
}
}
$str = 'Welcome to <h1><b></b></h1>';
if(hasTags($str)){
echo 'HTML tags Found';
}else{
echo 'HTML tags not found';
}
If you want to get the present html tags count then consider the below function.
// Function for check html tag presents
function hasTags($str){
$htmlTags = array('A' ,'B', 'BODY', 'BR', 'BUTTON', 'DIV', 'FONT', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'IMG', 'INPUT', 'TABLE');
// Here you mention what are the html tags you want to check
if(preg_match_all("~(<\/?)\b(".implode('|',$htmlTags).")\b([^>]*>)~i",$str,$matches)){
return $matches[1];
}else{
return 0;
}
}
$str = 'Welcome to <h1><b></b></h1>';
echo 'Tags Count = '.hasTags($str);