douzhan1994 2016-10-21 14:00
浏览 50
已采纳

php preg_match与两个div值之间得到

How can I get the value of this text. Idea: Year: 2012 KM: 69.000 Color: Blue Price: 29.9000

preg_match('@</div></td><td
 class=\"searchResultsAttributeValue\">(.*?)<\/td>@si',$string,$val);

 $string = "<div class="classifiedSubtitle">Opel > Astra > 1.4 T Sport</div>
</td>
            <td class="searchResultsAttributeValue">
                    2012</td>
            <td class="searchResultsAttributeValue">
                    69.000</td>
            <td class="searchResultsAttributeValue">
                    Blue</td>
            <td class="searchResultsPriceValue">
                        <div> $ 29.900 </div></td>
                <td class="searchResultsDateValue">
                        <span>21 Nov</span>
                        <br/>
                        <span>2016</span>
                    </td>
                <td class="searchResultsLocationValue">
                        USA<br/>Texas</td>"
  • 写回答

1条回答 默认 最新

  • dowb58485 2016-10-21 14:10
    关注

    The best solution isn't with regex. You should do it with Dom.

    $dom = new DOMDocument();
    $dom->loadHTML($string);
    $xPath = new DOMXpath($dom);
    $tdValue = $xPath->query('//td[@class="searchResultsAttributeValue"]')->get(0)->nodeValue;
    

    This way you'll get the td element with the class searchResultsAttributeValue. Of course you should verify if this element really exists, and some other verifications but that's the way.

    Hope I was helpful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效