duangan6731 2014-02-05 19:20
浏览 93
已采纳

在preg_match中使用多行字符串

i have the following problem: I try to find a part of a website using preg_match:

preg_match("|<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=5 WIDTH=1280 HEIGHT=50>
<TR VALIGN=TOP>
<TD WIDTH=1280 BGCOLOR=WHITE>
<FONT COLOR=BLACK SIZE=2>
This
is
a
test
</FONT>
</TR>
</TABLE>
|",$website,$matches);

It works fine... But the value of the table (in this case "This is a test") changes every day so i tried to do it like this:

preg_match("|<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=5 WIDTH=1280 HEIGHT=50>
<TR VALIGN=TOP>
<TD WIDTH=1280 BGCOLOR=WHITE>
<FONT COLOR=BLACK SIZE=2>
(.*)
</FONT>
</TR>
</TABLE>
|",$website,$matches);

But now the return value of the function is 0, so it didn't found any matches. Only when i try this it works again:

preg_match("|<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=5 WIDTH=1280 HEIGHT=50>
<TR VALIGN=TOP>
<TD WIDTH=1280 BGCOLOR=WHITE>
<FONT COLOR=BLACK SIZE=2>
(.*)
(.*)
(.*)
(.*)
</FONT>
</TR>
</TABLE>
|",$website,$matches);

So my problem is now: How can I find the part even if the value of the table has 4 lines today and 6 tomorrow for example (i never know^^)

Thank You

  • 写回答

4条回答 默认 最新

  • download2014711 2014-02-05 19:25
    关注

    You need the s modifier to match over multiple lines, see the manual:

    ...
    |s",$website,$matches);
    

    However, regex is not the best way to parse html, you'd better use something like PHP Simple HTML DOM parser.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度