在 PHP 的 HTML 解析中使用正则表达式


using regular expression in HTML parsing in php

嗯,这是html代码:

<?
    $html ='<table cellspacing="0" cellpadding="0" class="mytable">
                                         <tr>
                                            <th class="first-td">Home <span>Live</span></th>
                                            <th>type</th>
                                            <th>Status</th>
                                          </tr>
                                                                              <tr>
                                            <td width="40%" class="first-td">text1</td>
                                            <td>info</td>
                                            <td class="status"><span class="identify">asdf2</span></td>
                                          </tr>
                                                                                <tr>
                                            <td width="40%" class="first-td">text2</td>
                                            <td>info</td>
                                            <td class="status"><span class="identify2">asdf</span></td>
    ';
    $pattern = '/<span class="identify">(.*?)<'/span>/im';
    preg_match_all($pattern, $html, $matches);
    print_r($matches);
    ?>

但我想要的是取回单词文本,这是标识类的单元格中的 td。或者,如果类是识别打印 1 时更容易,或者如果它是识别 2,请按 0。

所以我像这样输入伪代码:

If class-identify print the td-class=first-td of that cell

我可以推荐你使用xpath,就像在这个例子中一样。