views:

267

answers:

1

Hi all!

I'm writing a parser for a scripting programming language in PHP. The syntax of that scripting language looks like this:

ZOMFG
&This is a comment
(show "Hello, World\!");

This is a page written in that language, that displays Hello, World! in the browser. But I could also have code like this:

ZOMFG
&This is a comment !
on multiple !
lines.
(show !
"Hello, !
World\!!
");

For now I use explode("\n", $content) to explode the page's content to an array which has each line of code in a separate index. So

ZOMFG
&This is a comment
(show "Hello, World\!");

becomes:

array('ZOMFG', '&This is a comment', '(show "Hello, World\!");');

When a line ends with a ! (except when the ! is escaped as \!), it should add that line, including the following line to the array as one single element. So

&This is a comment !
on multiple !
lines.

becomes

&This is a comment on multiple lines.

Does anyone know how to do this? Thanks in advance.

+1  A: 

you should be able to use preg_split with a negative lookbehind.

(i'm finding an example)

$lines = preg_split('|(?<!\!)\n|', $code);

As per comments, aware that this causes both the newline characters and the ! to remain. Can't see an easier solution to this than str_replace at the moment - but imagine there is one ... !

benlumley
nice idea, but that would not have the effect of stripping the !\n combination from the joined lines though.
Paul Dixon
Thanks man that worked. One problem: the ! remains in the middle of the line. Does anyone know how to fix that?
Time Machine
str_replace('\!', "!", $line);
Time Machine
Thanks, myself! That worked :p
Time Machine