doukuang6795 2013-04-01 17:50
浏览 155
已采纳

preg_match_all返回一个空数组

I'm developing a WordPress Plugin and I've ran into a serious problem with getting the correct information from a regular expression.

Code for the regular expression is below:

preg_match_all("|grass1(.*)grass2|i", $split[1], $out);

The output is an empty array.

$split[1] is the body of the email (as a string).

I'm looking to extract all the information between grass1 & grass2. So in the body of the email(as a string) there exists "grass1 Something Something grass2" which I need Something Something as an output. This is where the problem lies and it's wierd because it works if the string is the subject, header or the entire email it works just fine. So I'm confused on what is going on. Any help on understanding the problem will be helpful.

Sample of the body of the email:

"_NextPart_000_0098_01CE2BC1.BFAF9200
 Content-Type: text/plain charset="us-ascii" Content-Transfer-Encoding: 7bit
 grass1 I'm testing the before or after function. If successful you should see all of           this message in a future email. grass2" 
  • 写回答

1条回答 默认 最新

  • draj840143 2013-04-01 18:05
    关注

    May be there're some newline " " between grass1 and grass2 so the "m" (multilines search) modifier can be useful:

    preg_match_all("|grass1(.*)grass2|im", $split[1], $out);
    

    See PHP manual reference.pcre.pattern.modifiers.php to the references of modifiers.

    For example the "s" ("." match " " too) can be an alternative:

    preg_match_all("|grass1(.*)grass2|is", $split[1], $out);
    

    ADD Just to be clear: "m" and "s" are similar but not the same:

    • "s" let the "."(dot) match the newline too, so ".*" match a multilines text
    • "m" apply the pattern to a multilines text, so "a b" match a line ending with a and begining with b, in other word I can use " " as pattern of search
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?