I have a var of a HTTP (craigslist) link $link
, and put the contents into $linkhtml
. In this var is the HTML code for a craigslist page, $link
.
I need to extract the text between <h2>
and </h2>
. I could use a regexp, but how do I do this with PHP DOM? I have this so far:
$linkhtml= file_get_contents($link);
$dom = new DOMDocument;
@$dom->loadHTML($linkhtml);
What do I do next to put the contents of the element <h2>
into a var $title
?
if DOMDocument
looks complicated to understand/use to you, then you may try PHP Simple HTML DOM Parser which provides the easiest ever way to parse html.
require 'simple_html_dom.php';
$html = '<h1>Header 1</h1><h2>Header 2</h2>';
$dom = new simple_html_dom();
$dom->load( $html );
$title = $dom->find('h2',0)->plaintext;
echo $title; // outputs: Header 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With