AlWard Posted January 12, 2008 Share Posted January 12, 2008 I have a particular challenging task, and I haven't been able to find an answer... though I am admittedly still in the learning phase of PHP (pretty good at perl though). I have a single block of HTML code that's very messy. It contains upwards of 100 <h2> elements, and I would like to find some way in which to loop through, counting the number of <h2>'s, and extracting the content along the way. Assuming that there isn't any way in which to directly explode() the string into an array (delimiting elements would vary).. is there any way to go through the string and just take out the contents of all the H2's and put them into another string or array? So, If I have this HTML sample.... Lots of non-linebreaked HTML code...<[b]h2>first H@ content</h2>[/b]... lots more non-linebreaked HTML code.... [b]<h2>another H2 tag's content</h2>[/b]... and so on up to at least 100 H2's How would I get each of these H2 tags' content into an array element, like $h2tags[0] - $h2tags[99] - or something like that? Note that there will be a varying number H2s; it won't always be 100. Many thanks in advance. Quote Link to comment Share on other sites More sharing options...
dsaba Posted January 12, 2008 Share Posted January 12, 2008 this isn't too hard.. you can use perl style regex with preg_match_all() All of these patterns will work where ~ is the delimiter ~<h2>((??!</h2>).)+)</h2>~is ~<h2>(.+?)</h2>~is ~<h2>([^<]+)</h2>~i $pat = '~<h2>([^<]+)</h2>~i'; preg_match_all($pat, $content, $out); print_r($out); //look at your outputted array, matches in subgroup 1 aka: $out[1] $numH2s = count($out[1]); see this tut: http://www.regular-expressions.info/repeat.html Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted January 12, 2008 Share Posted January 12, 2008 alternative without regex <?php $string = ""; $clean = strip_tags($string,"<h2>"); $clean = explode("<h2>",str_replace("</h2>",$clean)); print_r($clean); ?> Quote Link to comment Share on other sites More sharing options...
dsaba Posted January 12, 2008 Share Posted January 12, 2008 str_replace requires 3 arguments: mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.