Jump to content

How To Preserve Empty Key in Explode?


bob33_14

Recommended Posts

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";

}

?>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

 

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.

Link to comment
Share on other sites

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 by bob33_14
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.