【问题标题】:What's wrong with this preg_match_all这个 preg_match_all 有什么问题
【发布时间】:2011-09-19 08:04:19
【问题描述】:

我正在使用file_get_contents 来读取具有表格的.html 文件。

<table id="someTable" style="width:100%;margin-bottom:0;">
    <tr style="display:none;">
        <td style="padding-left:25px;">Some text</td>
    </tr>
    <tr style="display:none;">
        <td style="padding-left:25px;">another text</td>
    </tr>
</table>

当我使用 preg_match_all 读取表格时,我数 $matches[1] 时一无所获

preg_match_all('/<table id="someTable" style="width:100%;margin-bottom:0;">(.*)<\/table>/', $html, $matches);
$co = count($matches[1]);

【问题讨论】:

  • 用正则表达式解析 HTML 几乎肯定不是可行的方法。您希望实现什么目标?

标签: php preg-match preg-match-all


【解决方案1】:

将修饰符 s 添加到您的 preg_match。

preg_match_all('/<table id="someTable" style="width:100%;margin-bottom:0;">(.*)<\/table>/s', $html, $matches);

http://ideone.com/3w0K2

【讨论】:

    猜你喜欢
    • 2019-09-10
    • 2012-05-26
    • 2021-08-20
    • 2015-09-23
    • 2021-02-27
    • 2012-03-10
    • 2010-11-01
    • 2011-08-26
    • 2020-04-16
    相关资源
    最近更新 更多