Monday 15 June 2015

php - Capture Blank Nodes DOM and assign value -



php - Capture Blank Nodes DOM and assign value -

ok i'm bit of newbie dom i've managed cobble semi working solution until now.

using xpath looking key elements within web page , looping through each instance fine, until reached node empty.

so when building array have 20 nodes of 1 element 14 of because img isn't there time.

so in effect have array looks this

array ( [0] => array ( [item] => pv10923 [img] => image1.jpg ) [1] => array ( [item] => pv10924 [img] => image2.jpg ) [2] => array ( [item] => pv10925 [img] => image3.jpg ) [3] => array ( [item] => pv10926 [img] => image4.jpg ) [4] => array ( [item] => pv10927 [img] => ) [5] => array ( [item] => pv10928 [img] => ) [6] => array ( [item] => pv10929 [img] => ) )

when in reality should

array ( [0] => array ( [item] => pv10923 [img] => image1.jpg ) [1] => array ( [item] => pv10924 [img] => image2.jpg ) [2] => array ( [item] => pv10925 [img] => ) [3] => array ( [item] => pv10926 [img] => ) [4] => array ( [item] => pv10927 [img] => ) [5] => array ( [item] => pv10928 [img] => image3.jpg ) [6] => array ( [item] => pv10929 [img] => image4.jpg ) )

now webpage source code looks this

<div id="item"> <h2>pv pv10924</h2> <p> <a href="http://www.example.com"><img src="image4.jpg"> </p> </div> <div id="item"> <h2>pv pv10925</h2> <p> &nbsp; (assign value) </p> </div> <div id="item"> <h2>pv pv10926</h2> <p> <a href="http://www.example.com"><img src="image5.jpg"> </p> </div>

ive been looking on see if there way capture parent if statement see if kid nowadays xpath if not assign node value x

being dyslexic reading isnot forte believe me i'm trying...

can please advise me on best route/method accomplish this....

you check descendants of particular element. example:

$sample_markup = '<div id="item"><h2>pv pv10924</h2><p><a href="http://www.example.com"><img src="image4.jpg"></a></p></div><div id="item"><h2>pv pv10925</h2><p>&nbsp; (assign value)</p></div><div id="item"><h2>pv pv10926</h2><p><a href="http://www.example.com"><img src="image5.jpg"></a> </p> </div>'; // using sample markup above $dom = new domdocument(); libxml_use_internal_errors(true); // handle errors $dom->loadhtml($sample_markup); libxml_clear_errors(); $xpath = new domxpath($dom); $data = array(); $elements = $xpath->query('//div[@id="item"]'); foreach($elements $e) { $item = $xpath->evaluate('string(.//h2/text())', $e); // checking $check = $xpath->evaluate('count(.//*[descendant::a])', $e); if($check > 0) { $image = $xpath->evaluate('string(.//a/img/@src)', $e); } else { $image = 'test.jpg'; } $data[] = array('item' => $item, 'image' => $image); } echo '<pre>'; print_r($data);

sample output

php html xpath scrape

No comments:

Post a Comment