开发者

PHP regexp: matching everything until a word appears

开发者 https://www.devze.com 2023-04-06 10:24 出处:网络
In PHP I\'m looking to match everything until a certain word appears: $text = \"String1 . testtesthephepString2 Here\";

In PHP I'm looking to match everything until a certain word appears:

$text = "String1

                         .

                  testtesthephepString2 Here";

$faultyregexp = "开发者_JAVA百科#String1(.+)String2 Here#";

preg_match($text, $faultyregexp, $result);

Now I want to match everything between String1 and String2, but for some reason this doesn't work.

I'm thinking you could do something like #String1(^String2 here+)String2 here# if you know what I mean :) ?


The issue is that by default . does not include newline characters. If you want . to match all character, you need to specify the s modifier (PCRE_DOTALL):

/String1(.+)String2 Here/s
0

精彩评论

暂无评论...
验证码 换一张
取 消