DOMDocument or HTML parser?

This is a discussion on DOMDocument or HTML parser? within the PHP Language forums, part of the PHP Programming Forums category; I want to extract the first <p> from a HTML, e.g. ================================== <?php $html = <<<...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-25-2008
howa
 
Posts: n/a
Default DOMDocument or HTML parser?

I want to extract the first <p> from a HTML, e.g.
==================================

<?php

$html = <<<EOT

<div>
<p> paragraph1 </p>

<p> paragraph2 </p>

<div id="test"></div>

</div>


EOT;

$doc = new DOMDocument();
$doc->loadHTML($html);

$lists = $doc->getElementsByTagName("*");

print_r($lists);

?>

==================================

Is it possible to do this using DOMDocument or other HTML parser
recommended?

Thanks.

Reply With Quote
  #2 (permalink)  
Old 02-26-2008
Toby A Inkster
 
Posts: n/a
Default Re: DOMDocument or HTML parser?

howa wrote:
> I want to extract the first <p> from a HTML, e.g.
> Is it possible to do this using DOMDocument or other HTML parser
> recommended?


$doc = new DOMDocument();
$doc->loadHTML($html);
$firstParagraph = $doc->getElementsByTagName("p")->item(0);

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 27 days, 17:30.]

Bottled Water
http://tobyinkster.co.uk/blog/2008/02/18/bottled-water/
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:34 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0