Jump to content

Taking Nth Item 's contents


blazing_bruce

Recommended Posts

Hello,

      I am learning regularexperssion. it is pretty awesome.

Is it possible to make a pattern in preg_match_all to grab content of 6th or 7th h1 tag's text

 

<h1 styles.. or any other attributes *>text</h1>

<h1 styles.. or any other attributes *>text</h1>

<h1 styles.. or any other attributes *>text</h1>

<h1 styles.. or any other attributes *>text</h1>

<h1 styles.. or any other attributes *>text</h1>

 

The main objective here is.. I am trying to find a text of Nth occurance of <h1 and </h1>

 

I was trying to use preg_match_all .. But unable to get it. Any help would be much appreciated . Im sorry if it is a silly question :)

Link to comment
https://forums.phpfreaks.com/topic/159300-taking-nth-item-s-contents/
Share on other sites

I don't think there's a preg_xxx, argument for a preg_xxx or a piece of regex itself where you can have it specifically return that.  However, what you could do is match for all <h1>...</h1> and they will be returned in order of matches found.  So you will know from $match that $match[0][6] or $match[1][6] would have the 7th match found.

 

 

The very idea of a site having that many h1s is hilarious (and bad SEO form at the same time). What do you intend to do with the nth occurrence?

Do you need just what <h1> holds? Or do you need the entire <h1> code? Lack of specifics means questioning what to capture (if there is a need to capture in the first place).

The very idea of a site having that many h1s is hilarious (and bad SEO form at the same time). What do you intend to do with the nth occurrence?

Do you need just what <h1> holds? Or do you need the entire <h1> code? Lack of specifics means questioning what to capture (if there is a need to capture in the first place).

 

Well yes too much <h1> is not a good. I am just trying to get contents inbetween tags.. I tried the following for <td> </td> .

preg_match_all('/<td [^>]*>((?:<td.+?<\/td|.)*?)<\/td>/si', $sometable, $matches);

 

It worked like charm :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.