preg_match 匹配 html 标签内文本的最简单方法

发布于 2024-11-26 22:58:19 字数 1220 浏览 0 评论 0原文

可能的重复:
使用 PHP 解析 HTML 的最佳方法

例如我有一个 html 代码like :

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="rowData">
  <tr align="center" class="fnt-vrdana-mavi" >
            <td style="font-size:11px" colspan=3><b>Text text text</b>:3</td>
        </tr>

        <tr class="header" align="center">
            <td height="18" colspan="3">Text text text</td>

        </tr>
        <tr align="center" class="fnt-vrdana" bgcolor="#eff3f4" height="18">
            <td width="32%" height="17"><b>1</b></td>
            <td width="34%"><b>0</b></td>
            <td width="34%"><b>2</b></td>
        </tr>
        <tr align="center" class="fnt-vrdana-mavi">

            <td height="17">2.90</td>
            <td>3.20</td>
            <td>1.85</td>
        </tr>
</table>

哪个是匹配 标签内所有数据的最佳正则表达式?

Possible Duplicate:
Best methods to parse HTML with PHP

for example i have a html code like :

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="rowData">
  <tr align="center" class="fnt-vrdana-mavi" >
            <td style="font-size:11px" colspan=3><b>Text text text</b>:3</td>
        </tr>

        <tr class="header" align="center">
            <td height="18" colspan="3">Text text text</td>

        </tr>
        <tr align="center" class="fnt-vrdana" bgcolor="#eff3f4" height="18">
            <td width="32%" height="17"><b>1</b></td>
            <td width="34%"><b>0</b></td>
            <td width="34%"><b>2</b></td>
        </tr>
        <tr align="center" class="fnt-vrdana-mavi">

            <td height="17">2.90</td>
            <td>3.20</td>
            <td>1.85</td>
        </tr>
</table>

Which is best regular expression to match all data from inside <td> tags?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过去的过去 2024-12-03 22:58:19

我通常建议,如果您需要在 HTML 文档中实际表达您要查找的内容,请使用 xpath 表达式,因为它可以为您提供实际值,而正则表达式无法进一步解析 HTML/XML,xpath 表达式更加细粒度。查看返回文本值的输出,例如内部没有任何其他标签:

array(8) {
  [0]=>
  string(16) "Text text text:3"
  [1]=>
  string(14) "Text text text"
  [2]=>
  string(1) "1"
  [3]=>
  string(1) "0"
  [4]=>
  string(1) "2"
  [5]=>
  string(4) "2.90"
  [6]=>
  string(4) "3.20"
  [7]=>
  string(4) "1.85"
}

代码:

$html = <<<EOD
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="rowData">
  <tr align="center" class="fnt-vrdana-mavi" >
            <td style="font-size:11px" colspan=3><b>Text text text</b>:3</td>
        </tr>

        <tr class="header" align="center">
            <td height="18" colspan="3">Text text text</td>

        </tr>
        <tr align="center" class="fnt-vrdana" bgcolor="#eff3f4" height="18">
            <td width="32%" height="17"><b>1</b></td>
            <td width="34%"><b>0</b></td>
            <td width="34%"><b>2</b></td>
        </tr>
        <tr align="center" class="fnt-vrdana-mavi">

            <td height="17">2.90</td>
            <td>3.20</td>
            <td>1.85</td>
        </tr>
</table>
EOD;

// create DomDocument to operate xpath on
$doc = new DomDocument;
$doc->loadHTML($html);

// create DomXPath
$xpath = new DomXPath($doc);

// perform the XPath query
$nodes = $xpath->query('//td');

// process nodes to return their actual value    
$values = array();
foreach($nodes as $node) {
    $values[] = $node->nodeValue;
}
var_dump($values);

I normally suggest if you need to actually express what you're looking for in a HTML document to use an xpath expression for that because it can give you the actual value whereas regex'es are not able to further parse the HTML/XML, and xpath expressions are much more fine-grained. See the output which returns the text-value for example w/o any further tags inside:

array(8) {
  [0]=>
  string(16) "Text text text:3"
  [1]=>
  string(14) "Text text text"
  [2]=>
  string(1) "1"
  [3]=>
  string(1) "0"
  [4]=>
  string(1) "2"
  [5]=>
  string(4) "2.90"
  [6]=>
  string(4) "3.20"
  [7]=>
  string(4) "1.85"
}

Code:

$html = <<<EOD
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="rowData">
  <tr align="center" class="fnt-vrdana-mavi" >
            <td style="font-size:11px" colspan=3><b>Text text text</b>:3</td>
        </tr>

        <tr class="header" align="center">
            <td height="18" colspan="3">Text text text</td>

        </tr>
        <tr align="center" class="fnt-vrdana" bgcolor="#eff3f4" height="18">
            <td width="32%" height="17"><b>1</b></td>
            <td width="34%"><b>0</b></td>
            <td width="34%"><b>2</b></td>
        </tr>
        <tr align="center" class="fnt-vrdana-mavi">

            <td height="17">2.90</td>
            <td>3.20</td>
            <td>1.85</td>
        </tr>
</table>
EOD;

// create DomDocument to operate xpath on
$doc = new DomDocument;
$doc->loadHTML($html);

// create DomXPath
$xpath = new DomXPath($doc);

// perform the XPath query
$nodes = $xpath->query('//td');

// process nodes to return their actual value    
$values = array();
foreach($nodes as $node) {
    $values[] = $node->nodeValue;
}
var_dump($values);
殊姿 2024-12-03 22:58:19

/(.*?)<\/td>/ 将获取 < 之间的所有数据;/td>

标签内部获取数据将是 /]*)>///

/<td.*?>(.*?)<\/td>/ would get all data between the <td> and </td>.

Getting the data from inside a <td> tag would be /<td([^>]*)>/ or /<td(.*?)>/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文