Reputation: 41
So I have been trying to figure out regex to capture multiple sections of my data but, seems I've been failing since last two days now...:(
Please help Let's say following is my data :
<div class="tabs_container">
<ul class="tabs">
##BEGINLOOP##gAs##
<li><a href="#">##title##</a></li>
##ENDLOOP##
</ul>
##BEGINLOOP##LI##
<div class="tabs_content">##title## is ##content##</div>
##ENDLOOP##
</div>
and i want to capture following group:
I have been trying various regex combination but nothing worked for giving me the exact solution. This was my last try
'/(?:##BEGINLOOP##)([a-z|A-Z]*)##(.+?)(?=##ENDLOOP##)(?:##ENDLOOP##)/s'
I am using PHP preg_match_all function
Please help ?
Upvotes: 0
Views: 57
Reputation: 99909
The regex is close to be right. Just a few notes:
[a-z|A-Z]
does not mean what you think it means. It means accept all chars from a to z, and the char |, and all chars from A to Z. You want [a-zA-Z]
.
(?=##ENDLOOP##)
means that the next group matches only if preceded by ##ENDLOOP##
, so ##ENDLOOP##
will match only if preceded by ##ENDLOOP##
. You may want to remove (?=##ENDLOOP##)
.
This works:
'/(?:##BEGINLOOP##)([a-zA-Z]*)##(.+?)(?:##ENDLOOP##)/s'
Try it here: http://3v4l.org/aCvvY
If you also want to also capture the parts outside of ##BEGINLOOP##...##ENDLOOP##, for the whole document, preg_split does the work:
$parts = preg_split('/(?:##BEGINLOOP##)([a-zA-Z]*)##(.+?)(?:##ENDLOOP##)/s', $subject, -1, PREG_SPLIT_DELIM_CAPTURE);
Try it here: http://3v4l.org/LWaQS
Upvotes: 2
Reputation: 6908
try:
preg_match('@(.*?)##BEGINLOOP##(.*?)##(.*?)##ENDLOOP##(.*?)@s', $data, $matches);
var_dump($matches);
Upvotes: 0