简体   繁体   English

换行符/在使用php检索的文本区域中返回

[英]Line breaks / returns in a text area retrieved with php

EDIT: My problem simplified I suppose has boiled down to not being able to completely remove the new line character. 编辑:我想简化的问题归结为无法完全删除换行符。

I tried: $new_content = str_replace("\\n", '<br/>', $_POST["new_content"]); 我尝试过: $new_content = str_replace("\\n", '<br/>', $_POST["new_content"]); And I can't see a reason why this wouldn't work.. but when I inspect the text file I still have a mysterious new line like so: 而且我看不到为什么这行不通的原因..但是当我检查文本文件时,我仍然有一个神秘的新行,如下所示:

event:|:Test:|:Line 1
<br/>Line 2:|:September 29th, 2012

Original question: 原始问题:

I have a form with a text field and a VERY simple 'database', that's just a .txt file, that i am storing the submitted form data in. It all works well aside from one major issue i've just noticed. 我有一个带有文本字段和非常简单的“数据库”的表单,这只是一个.txt文件,我将提交的表单数据存储在其中。除了我刚刚注意到的一个主要问题之外,所有这些都很好用。 If a user presses the enter/return key to go to a new line in the text box, whatever the resultant string is it messes up my text file where a new line signifies another row in the database. 如果用户按Enter / Return键转到文本框中的新行,则无论结果字符串是什么,它都会弄乱我的文本文件,其中新行表示数据库中的另一行。

For examples here's what the text file typically should look like: 例如,以下是文本文件通常的外观:

type:|:heading:|:content:|:date
event:|:Big Event 2012:|:More info and booking details.:|:September 29th, 2012
event:|:Website relaunch:|:Keep your eyes pealed.:|:September 25th, 2012

Now if i were to fill out the form pressing the return key in the text box to start a new paragraph my text file looks like this: 现在,如果我要填写表格,请在文本框中按回车键以开始新段落,我的文本文件将如下所示:

type:|:heading:|:content:|:date
event:|:Test:|:paragraph 1
paragraph 2:|:September 29th, 2012
event:|:Website relaunch:|:Keep your eyes pealed.:|:September 25th, 2012

As you can see the string has resulted in a new row being used in the database whereas i would expect / desire something like.. 如您所见,该字符串导致数据库中正在使用新行,而我希望/希望得到类似的内容。

event:|:Test:|:paragraph 1 \n paragraph 2 :|:September 29th, 2012 //or
event:|:Test:|:paragraph 1 <br/> paragraph 2 :|:September 29th, 2012

I did try looking for similar questions online but all the solutions pointed towards using something like nl2br($_POST["new_content"]); 我确实尝试过在线寻找类似的问题,但是所有解决方案都指向使用类似nl2br($_POST["new_content"]); but that seems to produce a text file formatted like this: 但这似乎会产生一个文本文件,其格式如下:

type:|:heading:|:content:|:date
event:|:Test:|:paragraph 1<br />

paragraph 2:|:September 29th, 2012
event:|:Website relaunch:|:Keep your eyes pealed.:|:September 25th, 2012

Where the original problem still persists. 原始问题仍然存在的地方。 Can someone explain why this happens or do you have any suggestions for how i should be formatting the string retrieved by $_POST["new_content"] before saving it in the text file? 有人可以解释为什么会发生这种情况吗?或者您对在将其保存到文本文件之前如何格式化$_POST["new_content"]得到的字符串有任何建议吗? Would appreciate any help! 将不胜感激!

Try 尝试

$_POST["new_content"] = str_replace("\n", '<br/>', $_POST["new_content"]);

Edit: 编辑:

You can also try: 您也可以尝试:

$_POST["new_content"] = ( preg_replace( '/\s+/', '<br/>', $_POST["new_content"] ) ); 

You have a fundamental issue of special characters. 您有一个特殊字符的基本问题。 A line break is a special character in your file format. 换行符是文件格式中的特殊字符。 You have two choices: either don't allow line breaks in the saved content, or escape/encode the line break in some way, for example by replacing it by \\n before writing into the file. 您有两种选择:要么不允许在保存的内容中使用换行符,要么以某种方式对换行符进行转义/编码,例如,在写入文件之前用\\n替换换行符。 That also means that \\n is a special character though, so you need to escape an actual \\n that may occur in the text somehow. 但这也意味着\\n是一个特殊字符,因此您需要转义可能会在文本中出现的实际\\n

See http://kunststube.net/escapism for more general information on special characters and escaping. 有关特殊字符和转义的更多常规信息,请参见http://kunststube.net/escapism You either need to come up with your own rules for your file format, or adopt some established format like XML. 您要么需要针对文件格式提出自己的规则,要么采用某些已建立的格式(例如XML)。

Try this on your processing file for saving new content into txt file: It's like @TheShiftExchange Answer just add new line break on variable content before saving it into txt file. 在处理文件上尝试将其保存为txt文件中的新内容:就像@TheShiftExchange Answer只是在将变量内容保存到txt文件之前添加新的换行符。

$file= 'log.txt';
$content= preg_replace('/\s+/', '', $_POST['new_content']);
$content.= "\n";
$fh= fopen($file, 'a+');
fwrite($fh, $content);
fclose($fh);
$all_contents= file_get_contents($file);
echo nl2br($all_contents);

Answer found in the documentation comments for nl2br: 在nl2br的文档注释中找到的答案:

strtr($text, array("\r\n" => '<br />', "\r" => '<br />', "\n" => '<br />'));

Source: http://php.net/manual/en/function.nl2br.php#91828 来源: http//php.net/manual/en/function.nl2br.php#91828

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM