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条)

报告相同问题?

悬赏问题

  • ¥15 如何在sql server里完成筛选
  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了