我有一个textarea
,用户最多可以写入1000个字符。我需要获得jQuery('#textarea').val()
并创建一个数组,其中每个项目是textarea
值的一行。这意味着:将textareas字符串值转换为由新行分隔的JavaScript数组
这是textarea中的一条不错的线。
这是另一行。
(让我们假设这条线是空的 - 它应该被忽略)。
有人在上面留下了2条以上的新行。
应转换为JavaScript数组:
var texts = [];
text[0] = 'This is a nice line inside the textarea.';
text[1] = 'This is another line.';
text[2] = 'Someone left more than 2 new lines above.';
这样,他们可以很容易地imploded用于向查询字符串(这是由提供者所需要的适量的格式):
example.com/process.php?q=["This is a nice line inside the textarea.","This is another line.","Someone left more than 2 new lines above."]
我尝试了phpjs explode()
和string.split("\n")
的方法,但他们没有照顾额外的新线(又名换行符)。有任何想法吗?
只是把我的头顶部,也许尝试拆分它然后喂养拆分成一个数组? –
@AndrewPeacock我刚更新了一些信息的帖子。我试过用phpjs explode(),但它没有照顾额外的行(如果有的话)的问题。 – andufo