简体   繁体   English

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

[英]using regular expression in HTML parsing in php

Well this is the html code: 嗯,这是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);

    ?>

But what I want is to get back the word text, which is the td in that cell where class is identify. 但是我想要的是找回文字一词,它是识别类的那个单元格中的td。 Or maybe if it is easier when class is identify print 1, or if it is identify2 press 0. 或者,如果在将class标识为print 1时比较容易,或者如果是identify2,则按0。

So I wna tin pseudocode like this: 所以我想这样的伪代码:

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

我可以建议您像本一样使用xpath。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM