DOM not parsing correctly

I have a script which loads items from an XML file, and displays them, so a user can choose which item they want to remove from their file. Here’s the PHP:

<?php
global $current_user;
get_currentuserinfo();

$userid= $current_user->ID;

$dom = new DOMDocument;
$dom->load("playlists/$userid.xml");

echo '<div class="styled-select">';
echo '<center><form name="input" action="/remove/removesure.php" method="get">';
echo '<select name="q[]" size="2" multiple>';

$titles = $dom->getElementsByTagName('title');
foreach ($titles as $title) {

    echo '<option>'.$title->nodeValue.'</option>';
}
echo '</select>';
echo '<input type="submit" class="submit" value="Remove">';
echo '</form></center>';
echo '</div>';
?>

The problem I’ve ran into is that it doesn’t display some objects correctly, mainly items with hyphens (it displays – instead of -) and titles with spaces at the end, and because of this, my removal code doesn’t find the item, and so can’t remove it. I don’t know what to do, and I don’t know why it’s doing this. I’m running the code in wordpress, if that makes a difference.

Read More

Any ideas?

Related posts

Leave a Reply

1 comment

  1. If there is no chance of having any kind of councurrency, I would suggest you to use the title index of the title tag as value of “option”. Eg:

    $titles = $dom->getElementsByTagName('title');
    $counter = 0;
    foreach ($titles as $title) {
        echo '<option value='.$counter.' >'.$title->nodeValue.'</option>';
        $counter++;
    }
    

    In removesure.php, you could handle this by using an XPath expression like the next one:

    //Title[2]
    

    where “2” is the index of the title that must be removed.

    That is a possible solution; another path you could try to follow is to handle spaces providing the best encoding option for your titles. htmlentity is the function that you should execute

    echo '<option>'.htmlentity($title->nodeValue).'</option>'