本帖最后由 setimouse 于 2011-02-14 18:26:46 编辑

解决方案 »

  1.   


    <?php 
    $str = '<table class="sp">
        <tr>
            <td>td1</td>
            <td>td2</td>
        </tr>
        <tr>
            <td>td3</td>
            <td>td4</td>
        </tr>
    </table>
    ';
    $regx = "/<td>(.*)<\/td>/isU";
    if (preg_match_all($regx,$str,$out)) {
    echo "OK";
    }else{
    echo "err";
    }
    print_r($out);
    如果比较少,可以用jquery哦。。
      

  2.   


    不是简单地匹配td中的内容,而是匹配table[class=sp]这样的表中的td内容。
      

  3.   

    $html='<table class="sp">
        <tr>
            <td>td1</td>
            <td>td2</td>
        </tr>
        <tr>
            <td>td3</td>
            <td>td4</td>
        </tr>
    </table>
    ';
    preg_match('@<table class="sp">(.*?)</table>@is',$html,$out);
    preg_match_all('/<td>(.*?)<\/td>/is',$out[1],$out);
    print_r($out[1]);
      

  4.   


    建议用simple_html_dom这个类来解析,因为你还可能要匹配其他的。
    谷歌搜索“PHP simple_html_dom”
      

  5.   

    preg_match_all('/<table class="sp">(.*?)<\/table>/s', $html, $matches1);
    preg_match_all('/<td>(.*?)<\/td>/', implode($matches1[1]), $matches2);
    var_dump($matches2[1]);
      

  6.   

    本采集规则 只要是table 包含有 class="sp" 就能被匹配到
    例如:
    <table border="0" class="sp" height="50" >
    <table class="sp" width="20" height="50" >
    <table border="0" style="color:#FF0000" class="sp" >
    <table class="sp">
    等等不论大小写 都可以匹配到。$html = <<<eof
    <table border="0"  ds class="sp" width="600" height="50">
        <tr>
            <td>td1</td>
            <td>td2</td>
        </tr>
        <tr>
            <td>td3</td>
            <td>td4</td>
        </tr>
    </table>
    eof;preg_match_all('/<table[^>]*(?:class="sp")[^>]*>(.*?)<\/table>/is', $html, $match);
    preg_match_all('/<td>(.*?)<\/td>/is', implode($match[1]), $matches);
    print_r($matches[1]);
      

  7.   


    分层匹配是比较合适的方法,即先匹配出来class="sp"的table, 再对其中的<td>标签进行匹配即可,试一下楼上的吧
      

  8.   

    不能用jquery,也不用simplehtmldom
    能否只用一个正则来解析,而不用2个?
      

  9.   

    preg_match_all('/\<table\s+class=\"sp\"\>(.*)\<\/table\>/',$str,$gcz);