nth occurrence of a pattern

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

nth occurrence of a pattern

Post by anjanesh »

Code: Select all

 
$xml = "
<starttag>
<starttag>...</endtag>
<starttag>...</endtag>
<starttag>...</endtag>
<starttag>...</endtag>
</endtag>
";
$pattern = "/<starttag>(.*?)[<\/endtag>]/is";
preg_match($pattern,$xml,$matches);
print_r($matches);
 
What do I have to change in the pattern to get the 5th occurrence of <endtag> ?

Thanks
Post Reply