How to access to the content of a html element using PHP? -
this question has answer here:
- how parse , process html/xml in php? 27 answers
i know how access content of html element using php. explanation below:
this html code:
<div id="edittitle" title="editing title"> <b>1</b> </div>
actually, want accessing content of <b>
tag (which "1") using php , putting in php variable. there idea how that?
thanks in advance.
the following code should give idea how can use domdocument - xpath not required really:
$strhtml=' <div id="edittitle" title="editing title"> <b>1</b> </div>'; libxml_use_internal_errors( true ); $dom=new domdocument('1.0','utf-8'); $dom->validateonparse=false; $dom->loadhtml( $strhtml ); libxml_clear_errors(); $col=$dom->getelementsbytagname('b'); echo $col->item(0)->nodevalue; /* output */ --> 1
to manipulate entire page below pseudo code should understand process. not tested of course ...
<?php @ob_start(); session_start(); include 'file.php'; include 'otherfile.pphp'; ?> <html> <head> <title>output buffering test</title> <link rel='stylesheet' href='style.css' /> <script type='text/javascript'> function blah(){ } </script> </head> <body> <h1>an attention grabbing headline</h1> <div id="edittitle" title="editing title"> <b>contents1</b> </div> </body> </html> <?php libxml_use_internal_errors( true ); $dom=new domdocument('1.0','utf-8'); $dom->validateonparse=false; $dom->loadhtml( @ob_get_contents() ); libxml_clear_errors(); $col=$dom->getelementsbytagname('b'); $your_variable=$col->item(0)->nodevalue; $buffer=$dom->savehtml(); @ob_end_clean(); echo $buffer; if( @ob_get_level() > 0 ) { for( $i=0; $i < ob_get_level(); $i++ ) @ob_flush(); @ob_end_flush(); } $dom=null; ?>
Comments
Post a Comment