我有一個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