简体   繁体   English

从textarea内的文本中删除空格

[英]remove whitespaces from text inside textarea

I have a textarea and inside it, looping through a unserialized array. 我有一个textarea并在其中,遍历一个未序列化的数组。

I am trying to take text inside of textarea, break the string into an array with new line spaces as a separator, and serialize the array and store back to database. 我正在尝试在textarea内获取文本,将字符串拆分为一个以新行空间作为分隔符的数组,并序列化该数组并存储回数据库。

But with these spaces, I am getting a whole string as a single array element. 但是有了这些空格,我得到了整个字符串作为单个数组元素。

Here's my code: 这是我的代码:

$items = unserialize($menu['lunch_items']);

<textarea name="items" class="editInput items form-control input-sm" style="display: none; resize: none;" rows="8" cols="60"><?php foreach($items as $item) :?>
<?php echo $item; ?>
<?php endforeach; ?></textarea>

Its output: 其输出:

                      Rice
Parbar Aalu saadheko bhuteko sab sab
Mix Curry (for veg)
Egg Curry (for non-veg)
Mula koreko with sano kerau

This is how I accessed the textarea value: 这是我访问textarea值的方式:

var items = $(this).closest("tr").find('textarea[name="items"]').val();

Here's how I get and try to break it into array: 这是我如何尝试将其分解为数组的方法:

$items = $postValues['items'];
print_r($items);

$itemArr = preg_split('/\r\n|\r|\n/', $items);
$itemArr1 = explode('\n', $items);
print_r($itemArr);
print_r($itemArr1);
die();

Output in console: 在控制台中输出:

Rice                                                            Parbar Aalu saadheko bhuteko sab sab                                                            Mix Curry (for veg)                                                         Egg Curry (for non-veg)                                                         Mula Koreko with sano kerau                         

Array
(
    [0] =>                              Rice                                                            Parbar Aalu saadheko bhuteko sab sab                                                            Mix Curry (for veg)                                                         Egg Curry (for non-veg)                                                         Mula Koreko with sano kerau                         
)
Array
(
    [0] =>                              Rice                                                            Parbar Aalu saadheko bhuteko sab sab                                                            Mix Curry (for veg)                                                         Egg Curry (for non-veg)                                                         Mula Koreko with sano kerau                         
)

What I want it to be: 我想要的是:

Array
(
   [0] => Rice
   [1] => Parbar Aalu sadheko bhuteko sab sab
   [2] => Mix Curry
    .
    .
)

How do I properly strip white spaces keeping new line spaces intact, and break it into proper array? 我如何正确地去除空格以保持新行空间完整,并将其分成适当的数组?

For your PHP-code, it would be reasonable to use the trim()-function after you generated the text-output. 对于您的PHP代码,在生成文本输出之后使用trim()函数是合理的。 Because you have an array here, it is easier to work with array_map(): 因为这里有一个数组,所以使用array_map()更容易:

$itemArr = array_map('trim', $itemArr);

This results is giving you back all Array elements trimmed. 这样的结果将给您所有修剪过的Array元素。 After this you can also implode the array to a string which you can put directly in your textarea: 之后,您还可以将数组内插为一个字符串,您可以将其直接放在文本区域中:

$itemArr = implode("\n", $itemArr);

To trim with JavaScript you can use the JavaScript-trim()-function: 要使用JavaScript修剪,可以使用JavaScript-trim()函数:

var textarea = $(this).closest("tr").find('textarea[name="items"]');
    textarea.val(textarea.val().trim());
$itemsArr = explode("\n", $items) ;

尝试这个

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

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