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

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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM