代码之家  ›  专栏  ›  技术社区  ›  beginnerprogrammer

prematch在a ref中获取文本

  •  -1
  • beginnerprogrammer  · 技术社区  · 8 年前

    HTML格式:

     <td class="td_class"><a href="javascript:goRead('115');" onmouseover="status='read';return true;" onmouseout="status=''" onfocus="blur()">Title</a></td>
    

    我需要让preg_匹配才能获得冠军,我已经尝试使用这个regex

    preg_match_all('/[^>]class=["\']td_class[\'"]*>(.*?)<\//',$result,$match);
        $datas['title'] = $match[1];
        var_dump($datas['title']);
    

    结果是

     <a href="javascript:goRead('115');" onmouseover="status='read';return true;" onmouseout="status=''" onfocus="blur()">Title</a>
    

    但我只想得到头衔,有人知道怎么做吗? 谢谢您!

    1 回复  |  直到 8 年前
        1
  •  1
  •   Alexandre Painchaud    8 年前

    DomDocument公司 效果很好, doc here .

    一个简单的例子

      //This steps is useful if you want to parse html of a website
      $html = file_get_contents('www.pathtohtml.com');
      $doc = new DOMDocument();
      //if you want to load html file you can use loadHtmlFile
      $doc->loadHTML($html); //This load html string
      $aTags = $doc->getElementsByTagName('a'); 
      foreach ($aTags as $aTag) {
        //$aTag->nodeValue this contain your A tag text node!
        //You can also access attributes ..
      }
    

    如果需要更精确地查询我建议的Dom XPATH .

    希望这有帮助。