How to remove links inside a specific class container (php scrape)?
I am using php to scrape a page. How do I remove links from within divs that have a specific class while keeping the name displayed?
e.g.
<p>Our list of teachers:</p>
<div class="teacher"><a href="...">John Brown</a></div>
<div class="teacher"><a href=".开发者_JS百科..">Peter Smith</a></div>
<div class="teacher"><a href="...">Jane Doe</a></div>
Thanks for any help, Geoff
DOMDocument (for parsing) and DOMXPath (for selecting) will allow you to easily get that text.
Use PHPQuery, a way of manipulating the DOM in a JQuery like manner.
phpQuery or QueryPath simplify this significantly. But to provide an answer with an actual example:
$dom = qp($html); // or $url
foreach ($dom->find("div.teacher a") as $a) {
$a->replaceWith( $a->text() );
}
$html = $dom->writeHTML();
精彩评论