Jump to content

Newlines disappearing when filling an array from xml


tyop

Recommended Posts

I'm having a problem with newline characters and nl2br(), Basically I have a form with a textarea, it reads from a XML file and puts the contents into the text area. Then changes can be made to the text, button clicked, XML updated.

 

Viewing the XML after it gets updated shows all my newline space, so I'm good at least up to that point. The problem is that when I try to display the text anywhere, I can't seem to get nl2br() to work for me.

 

Here's the relevant code from the form page.

<?php
function start_tag($parser, $name, $attrs){
global $display_string;
global $name;
$display_string .= "$attrs[lyrics]";
$name .= "$attrs[title]";
}
function end_tag($parser, $name){}
$parser = xml_parser_create('utf-8');
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($parser, "start_tag", "end_tag");
xml_parse($parser, file_get_contents("../lyrics/losingyou.xml")) or die("Error parsing XML file");

?> 
<form action="processSong.php" method="post">
<fieldset>
<label for="title">Title:</label>

<?php echo"<input type='text' id='title' name='title' value='" . $name . "' />"; ?>

<br />

<label for="path">Lyrics:</label> 
<textarea rows="30" cols="60" id="lyrics" name="lyrics">
<?php

echo nl2br($display_string);
?>
</textarea><br />
</fieldset>
<select name="action">
<option value="edit" checked="checked">edit</option>
</select>
<input type="submit" value="Save changes">
</form>

 

Here's the script that processes the form:

 

<?php
$songs = Array();
function start_element($parser, $name, $attrs){
global $songs;

}
function end_element ($parser, $name){}
$playlist_string = file_get_contents("../lyrics/losingyou.xml");
$parser = xml_parser_create('UTF-8');
xml_set_element_handler($parser, "start_element", "end_element");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse($parser, $playlist_string) or die("Error parsing XML document.");
print "<br />";
if($_POST['action'] == "edit"){
array_push($songs, Array(
			"title" => $_POST['title'],
			"lyrics" => $_POST['lyrics']));
$songs_final = $songs;
} else die ("Error message: Your error had an error in it's error or something.");
$write_string = "<songs>";
foreach($songs_final as $song){
$words = $song[lyrics];
$write_string .= "<song title=\"$song[title]\" lyrics=\"$words\" />";
}
$write_string .= "</songs>";
$fp = fopen("../lyrics/losingyou.xml", "w");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
print "<em>Playlist edited successfully</em><br />";
print "<a href=\"edit_lyrics.php\" title=\"return\">Return</a>";
?>

 

I've tried getting my <br /> in there a few different ways but none seem to work right. I don't know if I should have it insert them into XML or not, I wouldn't know how to make it parse right.

 

Can someone tell me where my newlines are going and how I can get them back so I can convert via nl2br for output?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.