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

报告相同问题?

悬赏问题

  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥100 华为ensp只要2-9实验运行结果能做的来加我QQ
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。
  • ¥15 已知隐函数其中一个变量τ的具体值,求另一个变量
  • ¥15 r语言Hurst指数
  • ¥15 Acrn IVSHMEM doorbell问题
  • ¥15 yolov5中的val测试集训练时数量变小问题