I'm trying to get all occurrences of regular expression using preg_match_all and then check if there is particular string in those occurrences. After that, I am trying to count and compare number of occurrences but it seems to me that it is not working. I'm working with HTML data taken from the database, and yes I really need regular expressions for HTML. No matter which data I take from the database the result is following: Image pregmatch count: 2Image search count: 1Table pregmatch count: 2Table search count: 1
This is my code snippet:
$query = $DB->get_field('book_chapters', 'content', array('bookid'=>'1'));
$img_pat = '/<img(.*)\>/i'; //regular expression for image tag search
$table_pat = '/<table(.*)\>/i'; //regular expression for table tag search
echo $query;
$content = serialize($query);
echo $content;
//image
preg_match_all($img_pat, $content, $img_pregmatch);
$img_search = array_search('alt="', $img_pregmatch);
echo 'Image pregmatch count: ' . count($img_pregmatch);
echo 'Image search count: ' . count($img_search);
//table
preg_match_all($table_pat, $content, $table_pregmatch);
$table_search = array_search('summary="', $table_pregmatch);
echo 'Table pregmatch count: ' . count($table_pregmatch);
echo 'Table search count: ' . count($table_search);
And this is example when using rubular.com
Any help, advice is appreciated, thanks!