Question

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?

Was it helpful?

Solution

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 ... !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top