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

 

 

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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 :)

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.