HartleySan Posted March 26, 2011 Share Posted March 26, 2011 Perhaps what I'm trying to do is not possible with regexes, but I'm gonna ask anyway, as more likely, I'm just incapable of thinking of a decent regex. Let's say we have the following code within an HTML file: <ul id="1"> <li>List item</li> <li>List item</li> <li>List item</li> <li>List item</li> <li>List item</li> <li>List item</li> </ul> <ul id="2"> <li>List item</li> <li>List item</li> <li>List item</li> <li>List item</li> <li>List item</li> <li>List item</li> </ul> <ul id="3"> <li>List item</li> <li>List item</li> <li>List item</li> <li>List item</li> <li>List item</li> <li>List item</li> </ul> I want to come up with a regex that'll capture all six <li> tags within the id="2" ul. Is this possible? Link to comment Share on other sites More sharing options...
Jonathon Posted March 26, 2011 Share Posted March 26, 2011 What are you capturing them for?? I think you can parse it XML using the parent node (thought not sure) 1 Link to comment Share on other sites More sharing options...
HartleySan Posted March 26, 2011 Author Share Posted March 26, 2011 That's a good point, except I don't know how to change a string containing HTML data into XML data. Do you? Also, the thought had crossed my mind of doing one regex search to grab the substring <ul id="2">...</ul>, and then parsing within that, but I suppose I'd rather just use one regex, if possible...if only for a challenge. Link to comment Share on other sites More sharing options...
HartleySan Posted March 26, 2011 Author Share Posted March 26, 2011 Also, I should mention that the reason I'm doing this is that I want to be able to dynamically count the number of li tags within certain uls. The other trick though is that I don't know how many lis there actually are, and it changes all the time. I suppose it would have been helpful if I had better explained the situation at the start. Sorry. Link to comment Share on other sites More sharing options...
Jonathon Posted March 26, 2011 Share Posted March 26, 2011 Yeah that seems like the way i'd go, using the RegEx to grab the data and then parse insde that. I don't have too much to do with RegExs so my experiences with them aren't vast. But I get by! 1 Link to comment Share on other sites More sharing options...
Stuart Posted March 26, 2011 Share Posted March 26, 2011 To answer your question - yes it is possible with a regular expression and its relatively straight forward - also depending on what you want to do with the values after you might be able to just do it client side with jQuery because it would be extremely simple. $('li [id="2"] > li').length; If you wanted to use regex the starting point would be extracting the list with an ID of 2 and then creating sub-expressions to grab the li elements. Then use preg_match_all to extract them and apply PHP's count to the relevant branch of the returned array. If you're having no luck getting it working post your regex and we can try and help you out. 1 Link to comment Share on other sites More sharing options...
HartleySan Posted March 27, 2011 Author Share Posted March 27, 2011 Yeah, I know jQuery makes it simple; almost too simple for my tastes. What I mean by that is that I want to challenge myself. Anyway, if I use two expressions, one to grab the id="2" ul, and one to parse the content, it's pretty simple. First I do a substring match with the following regex: <ul id="2">[\s\S]*?<\/ul> And then I simply do I global search for the li tags, as such: <li> Well, the pattern is /<li>/g, but the PHP functions work differently. Anyway, my point is, that's a piece of cake, but I'm thinking that where two expressions suffice, there must be a way to do it with just one. Link to comment Share on other sites More sharing options...
Recommended Posts