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.
<?php
function start_tag($parser, $name, $attrs){
global $display_string;
global $name;
$display_string .= "$attrs[words]";
$name .= "$attrs[heading]";
}
function end_tag($parser, $name){}
$parser = xml_parser_create();
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("../file.xml")) or die("Error! You screwed up again.");
?>
<form action="process.php" method="post">
<fieldset>
<label for="heading">heading: </label>
<?php echo"<input type='text' id='heading' name='heading' value='" . $name . "' />"; ?>
<br />
<label for="words">words: </label>
<textarea rows="30" cols="60" id="words" name="words">
<?php
echo nl2br($display_string);
?>
</textarea><br />
</fieldset>
<select name="action">
<option value="edit">edit</option>
</select>
<input type="submit" value="Save changes">
</form>
That's the relevant part of the php and html for the input/edit form.
?php
$things = Array();
function start_element($parser, $name, $attrs){
global $things;
}
function end_element ($parser, $name){}
$some_string = file_get_contents("../file.xml");
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "end_element");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse($parser, $some_string) or die("Error parsing XML document.");
print "<br />";
if($_POST['action'] == "edit"){
array_push($things, Array(
"heading" => $_POST['heading'],
"words" => $_POST['words']));
$things_final = $things;
} else die ("Error message: Your error had an error in it's error or something.");
$write_string = "<things>";
foreach($things_final as $thing){
$words = $thing[words];
$write_string .= "<thing heading=\"$thing[heading]\" words=\"$words\" />";
}
$write_string .= "</things>";
$fp = fopen("../file.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.php\" title=\"return\">Return</a>";
?>
That's the script that processes things. >.>
The xml is pretty straightforward.
<?xml version="1.0" encoding="UTF-8" ?>
<things>
<thing
heading="whatever"
words="stuff and things" />
</things>
Am I breaking the array somehow?
Edit: 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.