I need help on regular expression here.
I want PHP to be able to split a string in sections of arrays such that a substring enclosed by <%me %> will be in its own slot.
So for example,
Hi there how are <%me date(); %> => {"Hi there how are ", "<%me date(); %>}
Hi there how are you<%me date(); %> => {"Hi there how are you", "<%me date(); %>}
Hi there how are you<%me date(); %>goood => {"Hi there how are you", "<%me date(); %>, "good"
Hi there how are you<%me date(); %> good => {"Hi there how are you", "<%me date(); %>}, " good"}
Note the white space won’t stop the tags from getting parsed in.
On capturing the splitting delimiter in
PREGYou can use
PREG_SPLIT_DELIM_CAPTUREto split on and capture the delimiter.Remember to put the delimiter in a capturing group
(…)for this to work properly.Here’s an example:
This prints (as seen on ideone.com):
References
preg-split–PREG_SPLIT_DELIM_CAPTUREBack to the question
In this case, you can try the delimiter pattern
(<%me[^%]+%>). That is:<%me, literally[^%]+, i.e. anything but%%>, literallyIf
%can appear in the tag, then you can try something like(<%me.*?%>).Here’s an example:
The above prints (as seen on ideone.com):
Related questions
.*?and.*for regex