bob33_14 Posted May 4, 2014 Share Posted May 4, 2014 I am scrapping an xml feed with the file_get_contents() and explode() function. Some the xml nodes do not exsist; so I am getting an inconsistant array from a for() loop. What would be the best way to keep an array key, created from an explode() function; empty, if there is no corresponding xml node to be scrapped? Currently, when I use the code below; I get all the node's content (that exsisit within the xml document) at the top and then, at the bottom; I get all the empty node's content or basically a empty/blank area. What I am getting is this: pic1.jpg<br/> pic3.jpg<br/> pic5.jpg<br/> <br/> <br/> And... what I want to get is this: pic1.jpg<br/> <br/> pic3.jpg<br/> <br/> pic5.jpg<br/> Here is my XML code (syndicate.xml): <?xml version="1.0" encoding="iso-8859-1"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:enc="http://purl.oclc.org/net/rss_2.0/enc#" xmlns:ev="http://purl.org/rss/1.0/modules/event/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:syn="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:admin="http://webns.net/mvcb/" > <item> <enc:enclosure resource="pic1.jpg" type="image/jpeg"/> </item> <item> </item> <item> <enc:enclosure resource="pic3.jpg" type="image/jpeg"/> </item> <item> </item> <item> <enc:enclosure resource="pic5.jpg" type="image/jpeg"/> </item> </rdf:RDF> And... here is my PHP code: <?php $rss = file_get_contents("syndicate.xml"); $img = explode("enclosure resource=\"",$rss); for($key = 0; $key < 5; $key++){ $img2 = explode("\"",$img[$key+1]); echo $img2[0]."<br/>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/288234-how-to-preserve-empty-key-in-explode/ Share on other sites More sharing options...
Ch0cu3r Posted May 4, 2014 Share Posted May 4, 2014 Use PHP's DOMDocument object to traverse through the xml structure, dont try parsing the xml structure yourself. Quote Link to comment https://forums.phpfreaks.com/topic/288234-how-to-preserve-empty-key-in-explode/#findComment-1478158 Share on other sites More sharing options...
bob33_14 Posted May 4, 2014 Author Share Posted May 4, 2014 Use PHP's DOMDocument object to traverse through the xml structure, dont try parsing the xml structure yourself. Yeah, I looked into that, but I am using a web host that will not let me install other extentions (like libxml). So I am trying to push the keys into dynamic array with explode. It works on other xml nodes (that have a consistant representation, throughout the xml document), but no so much on no exsistant nodes. Is the another way to go about this? So far, I have tried checking the strlen() of $img2[0] (in a if...else condition); but that did not seem to work. Quote Link to comment https://forums.phpfreaks.com/topic/288234-how-to-preserve-empty-key-in-explode/#findComment-1478159 Share on other sites More sharing options...
Ch0cu3r Posted May 4, 2014 Share Posted May 4, 2014 Yeah, I looked into that, but I am using a web host that will not let me install other extentions (like libxml). It is built into PHP you dont need to enable/install any extensions for it to work You can check it is available by running phpinfo() if lists dom and libxml then you should able to use DOMDocument object. Quote Link to comment https://forums.phpfreaks.com/topic/288234-how-to-preserve-empty-key-in-explode/#findComment-1478160 Share on other sites More sharing options...
bob33_14 Posted May 4, 2014 Author Share Posted May 4, 2014 (edited) It is built into PHP you dont need to enable/install any extensions for it to work You can check it is available by running phpinfo() if lists dom and libxml then you should able to use DOMDocument object. Your right; I just looked at the phpinfo() and DOM is available with libxml; thank you for the help. Is there a good tutorial, that you might suggest for DOMDocument object? Edited May 4, 2014 by bob33_14 Quote Link to comment https://forums.phpfreaks.com/topic/288234-how-to-preserve-empty-key-in-explode/#findComment-1478161 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.