php - Pregmatch and count giving bad result -
I am trying to get all the occurrences of regular expressions using preg_match_all and then there are specific strings in those events. or not. After that, I am trying to compute and compare numbers but I think it is not working. I am working with the HTML data taken from the database, and yes, I need regular expressions for HTML. Any data I get from a database, the result is as follows: Image pregme number: 2Image search count: 1Table pregmatch count: 2Table search count: 1 This is my code snippet : And this example is when using rubular.com Any help, advice is appreciated, thanks! Try to change your matters: Note: You should not use it because HTML is not an regular language .
$ query = $ DB- & gt; Get_field ('book_chapters', 'content', array ('bookid' = & gt; '1')); $ Img_pat = '/<img(.*)\>/i'; // regular expression for image tag search $ table_pat = '/<tabletable.edu.exe; // regular expression for table tag search query echo $ query; $ content = serialize ($ query); Counterpart $ content; // image preg_match_all ($ img_pat, $ content, $ img_pregmatch); $ Img_search = array_search ('alt =' ', $ img_pregmatch);' Image pregme 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); Count echo 'table pregmatch:' Count ($ table_pregmatch); Echo 'table search count:'. Count ($ table_search);
preg_match_all () will return an array of capture groups, then
$ Img_pregmatch [0] will have all your matches and will return
$ img_pregmatch [1] to your first capture groups.
'Image pregme number:' echo count ($ img_pregmatch [0]); Count echo 'table pregmatch:' Count ($ table_pregmatch [0]);
Comments
Post a Comment