I created a simple templating/content parser where you can specify where blocks of text should be placed in the final output page. The “markup” I settled on is something like @blockname> on it’s own line. So to put a block of text in the body, you would start the text with @body>. The blockname can be anything you want to use, but it can’t contain spaces or line breaks.
Everything works fine, except if there is a text string like @body> embedded in the text. It splits on the embedded text too. This seems really simple, I just want to match a line that starts with @ followed by any non-white space characters and ending with > at the end of the line. The command I have right now that almost works is:
preg_split('/@([^\s].*?[^>])>/', $tpl, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
It seems I should just be able to use ^@([^\s].*?[^>])>$, but that doesn’t work at all. I feel I’m missing something simple. I’ve tried a bunch of modifiers at the end to no avail.
Example text to parse:
@css>
.example {font-weight:bold;}
@js>
function example() {
alert('example');
}
@body>
This is some sample @body> text to show
That ideally the regex split would come back with:
Array
(
[0] => css
[1] =>
.example {font-weight:bold;}
[2] => js
[3] =>
function example() {
alert('example');
}
[4] => body
[5] =>
This is some sample @body> text to show
You can do that with:
With PHP quoting. You were probably missing the
/mswitch which makes^/$match beginning/end of lines, and not only strings.