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 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)