blazing_bruce Posted May 22, 2009 Share Posted May 22, 2009 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 More sharing options...
.josh Posted May 22, 2009 Share Posted May 22, 2009 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 https://forums.phpfreaks.com/topic/159300-taking-nth-item-s-contents/#findComment-840177 Share on other sites More sharing options...
nrg_alpha Posted May 22, 2009 Share Posted May 22, 2009 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 https://forums.phpfreaks.com/topic/159300-taking-nth-item-s-contents/#findComment-840197 Share on other sites More sharing options...
Axeia Posted May 22, 2009 Share Posted May 22, 2009 Sounds more like a job for xpath. Link to comment https://forums.phpfreaks.com/topic/159300-taking-nth-item-s-contents/#findComment-840243 Share on other sites More sharing options...
blazing_bruce Posted May 26, 2009 Author Share Posted May 26, 2009 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 https://forums.phpfreaks.com/topic/159300-taking-nth-item-s-contents/#findComment-842706 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.