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.

Lets see if i understand what you're trying to attempt.

You have a physical xml file somewhere on your server, we'll call it file.xml and you have a php form. That should load the contents of file.xml and allow the user to add/subtract from it, and then a save button that takes whats in the text box and then overwrites the file?

For starters the editing part does not require you to parse the xml.
user file_get_contents('file.xml') to load the contents of the file into a variable and into the textarea, or use something like this:

$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        echo $buffer;
    }
    fclose($handle);
}

That code will read the file line by line and only display one line at a time until it has reached the end of the file. It's much more efficient if you're trying to display a large file then loading the entire file into memory with file_get_contents and then displaying it.

When you go to save it, you dont need to convert the newlines to <br> in the xml. If the xml looks correct in the textarea it will save just like that into the text file. Now, if the content in your xml has <br /> tags thats a different story, as to be valid XML that node would need to be enclosed in a CDATA structure or would need the < & >'s encoded.

Displaying text from the XML is where things can get tricky especially with PHP4. If you are using php5, which you should be by now, then you should take a look at the Simple XML documentation on php.net.

http://us3.php.net/manual/en/function.simplexml-element-construct.php

I can't really help you any further with simple XML as it would require knowing what your xml structure looks like.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.