在php中处理新行 [英] Handling new lines in php

查看:75
本文介绍了在php中处理新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在html中有表单,用户可以在其中将文本放在文本区域中.
我将文本区域的内容保存到MySQL数据库中(在TEXT类型的字段中).
然后,我需要在应用程序中的某个地方加载该文本,并将其放入数组中,其中每个索引将是文本的一行.

I have form in html where user can put the text in text area.
I save the content of text area into MySQL database (in field of type TEXT).
Then I somewhere in my aplication I need load that text and put it into array where in each index will be one line of the text.

<textarea rows="10" cols="80">
Row one
Row two
Row tree
</textarea>

array (output in "php pseudocode"):
$array = array();
$array[0] = Row one;
$array[1] = Row two;
$array[2] = Row tree;

我该怎么做: 我将其保存到数据库,然后加载并使用:

How I do it: I save it to db then I load it and use:

$br = nl2br($string,true);
$array = explode("<br />", $br);

我使用 nl2br 的原因是我想避免来自不同平台的文本行尾出现问题.我的意思是/n或/r/n.
但是在我的解决方案中一定是某个错误导致原因不起作用(数组$ array为空).

更好的解决方案可能是以某种方式使用explode或类似的方式将其拆分为数组,并使用换行符模式,但这又是换行符开头的问题,我不知道如何解决(问题,\ n或\ r \ n ).

有人可以给我建议吗?谢谢.

The reason why I use nl2br is I want to avoid problems with end of lines of the text from different platforms. I mean /n or /r/n.
But in my solution must be an error somewhere cause it doesn't work (an array $array is empty).

Better solution would be probably somehow split it into array using explode or something like that with pattern for line breaks but here is again the problem from beginning with line breaks which I don't know how to solve (problems with \n or \r\n).

Can anybody give me an advice? Thanks.

推荐答案

我建议您跳过nl2br,直到准备好将数据实际发送到客户端为止.解决您的问题:

I'd suggest you skip the nl2br until you're ready to actually send the data to the client. To tackle your problem:

// $string = $get->data->somehow();
$array = preg_split('/\n|\r\n/', $string);

这篇关于在php中处理新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆