Php regex analysis block

I am trying to write (I think) a fairly simple RegEx with PHP, but it does not work. Basically, I have a block defined as follows:

%%%%blockname%%%%
stuff goes here
%%%%/blockname%%%%

I do not use RegEx, but this is what I tried:

preg_match_all('/^%%%%(.*?)%%%%(.*?)%%%%\/(.*?)%%%%$/i',$input,$matches);

It returns an array with 4 empty elements.

I believe that, in addition to the work itself, you need some kind of pointer for the third match, because it should be equal to the first?

Please enlighten me :)

+3
source share
2 answers

You need to resolve the points of coincidence with a new line character and resolve compliance ^and $at the beginning and end of lines (and not only the whole of the line):

preg_match_all('/^%%%%(.*?)%%%%(.*?)%%%%\/(.*?)%%%%$/sm',$input,$matches);

The s(single line) parameter makes the dot match any character, including newlines.

m () ^ $ .

i , , .

, : blockname , , :

preg_match_all('/^%%%%(.*?)%%%%(.*?)%%%%\/\1%%%%$/sm',$input,$matches);
+8

All Articles