preg_match_all and newlines inside quotes

安稳与你 提交于 2019-12-11 01:48:10

问题


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"
        )
    ...
}

回答1:


The m modifier does not make the dot match newlines. That's what the s modifier is for. m makes ^ and $ match start/end of lines in addition to start/end of string.

Try /(\'|")((?:(?!\1).)*)\1/s

Explanation:

(\'|"): Match a single or double quote. Remember which one it was in backreference \1.

(?:(?!\1).): Match any character as long as it's not the opening quote character (in \1).

(...*): Repeat as often as possible and capture the match in backreference \2.

\1: Match the opening quote character.




回答2:


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

$s = preg_replace('/<\?php|\?>/sm',"",$str);
$s = preg_split("/.*=/",$s);
print_r($s);


来源:https://stackoverflow.com/questions/2499127/preg-match-all-and-newlines-inside-quotes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!