Wednesday 15 May 2013

html - php get word between two different tags -



html - php get word between two different tags -

i parsing website using domdocument class.

the html code this

<img src="xxxxxx"> abc <br> <img src="xxxxxx"> def <br> .....

what want extract img src link , words between <img> tag , <br> tag pair. looping pairs within html code.

i able <img> src using getelementsbytagname have no thought how can words between img , br tag.

is there method pair img src , words?

thanks

if want target text after, each img tags.

then after point next sibling:

->nextsibling

rough example:

$sample_html = ' <img src="http://www.whatever.com" alt="" /> abc <br> <img src="http://goingnowhere.com" alt=""> def <br>'; $dom = new domdocument(); $dom->loadhtml($sample_html); $data = array(); $images = $dom->getelementsbytagname('img'); foreach ($images $image) { $data[] = array( 'src' => $image->getattribute('src'), 'text' => trim($image->nextsibling->textcontent), ); } echo '<pre>'; print_r($data);

now should like:

array ( [0] => array ( [src] => http://www.whatever.com [text] => abc ) [1] => array ( [src] => http://goingnowhere.com [text] => def ) )

php html domdocument

No comments:

Post a Comment