Reputation: 25392
I am using a form to submit a list of items that I want to save to a config text file, but these list items should all be on the same line of the text file. Every time PHP breaks apart and puts the list items into the file, however, it automatically adds a new line before the </li>
tag. Why? How do I prevent this.
CODE:
$pro = explode("\n",$_POST["pro"]);
$proo = "<ul>";
for($index=0; $index<count($pro);$index++)
{
$proo .= "<li>";
$proo .= $pro[$index];
$proo .= "</li>";
}
$proo .= "</ul>";
$proo = str_replace("\n","",$proo);
Edit 1
If I echo $proo
into the webpage, it looks like this:
<ul><li>List Item 1
</li><li>List Item 2
</li><li>List Item 3
</li><li>List Item 4
</li><li>List Item 5</li></ul>
Why are there line breaks before each </li>
?
Somewhere in there, it's adding new lines...
Thanks!
David
Upvotes: 0
Views: 165
Reputation: 5211
In any system you are using PHP_EOL will aways equal to the appropriate End-Of-Line-char-sequence.
explode(PHP_EOL,$_POST["pro"]);
The code above will always work.
The line breaks (PHP_EOL values) are:
Windows: \r\n
*nix: \n
old Apple Macintosh's (new ones are based on unix): \r
PHP_EOL doc
Edit:
as pointed out by Dagon, browsers are using Windows' line-breaks by default.
explode("\r\n",$_POST["pro"]);
is the way to go then
Upvotes: 0
Reputation:
line breaks can be tricky windows or linux \n
and or \r
so i added a trim to remove anything extra after the explode, seems to work. personally i would use a foreach() father than for() but thats just me
$_POST["pro"]="1
2
3
4";
$pro = explode("\n",$_POST["pro"]);
$proo = "<ul>";
for($index=0; $index<count($pro);$index++)
{
$proo .= "<li>".trim($pro[$index])."</li>";
}
$proo .= "</ul>";
echo $proo;
output:
<ul><li>1</li><li>2</li><li>3</li><li>4</li></ul>
Upvotes: 4