Jump to content

Extract Multiple <H2> Tags from single string


AlWard

Recommended Posts

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.