preg_match_all and newlines inside quotes

Posted by David on Stack Overflow See other posts from Stack Overflow or by David
Published on 2010-03-23T10:37:27Z Indexed on 2010/03/23 10:53 UTC
Read the original article Hit count: 366

Filed under:
|
|

Another noob regex problem/question. I'm probably doing something silly so I thought I'd exploit the general ingenuity of the SO regulars ;)

Trying to match newlines but only if they occur within either double quotes or single quotes. I also want to catch strings that are between quotes but contain no newlines.

Okay so there's what i got, with output. Below that, will be the output I would like to get. Any help would be greatly appreciated! :)

I use Regex Coach to help me create my patterns, being a novice and all. According to RC, The pattern I supply does match all occurances within the data, but in my PHP, it skips over the multi-line part. I have tried with the 'm' pattern modifier already, to no avail.

Contents of $CompressedData:

<?php
$Var = "test";
$Var2 = "test2";
$Var3 = "blah blah
blah blah blah
blah blah blah blah";
$Var4 = "hello";
?>

Pattern / Code:

preg_match_all('!(\'|")(\b.*\b\n*)*(\'|")!', $CompressedData, $Matches);

Current print_r output of $Matches:

Array
(
    [0] => Array
        (
            [0] => "test"
            [1] => "test2"
            [2] => "hello"
        )
    ...
}

DESIRED print_r output of $Matches:

Array
(
    [0] => Array
        (
            [0] => "test"
            [1] => "test2"
            [2] => "blah blah
blah blah blah
blah blah blah blah"
            [3] => "hello"
        )
    ...
}

© Stack Overflow or respective owner

Related posts about regex

Related posts about php