如何使用file_get_content()搜索html并检索特定的DIV类


How to search html using file_get_content() and retrieve a specific DIV class?

大家好。

我想file_get_content()加载一个网页,并使用strip_tags()来获取字符串:Category:Apple

<div class="category">
    <span style="font-size:11px; font-weight:bold;">Category:</span>
           <a href="/listing/A/new/yp/search.do?applicationInd=A" 
                class="category">Apple
           </a>
</div>

$text = '<div class="category">
         <span style="font-size:11px; fontweight:bold;">
         Category:</span><a href="/listing/A/new/yp/search.do?applicationInd=A"
         class="category">Apple</a></div>';
         echo strip_tags($text); //Category:Apple

我需要做什么php语句来传递变量到$text ?

使用preg_match吗?

试试这个

preg_match('|<a href="([^"]*?)" class="category">([^>]*?)<'/a>|smi', $text, $matches);
$cat = "Category:" . $matches[2];
echo $cat;