簡體   English   中英

如何使用PHP從數組中刪除空格?

[英]How do you strip whitespace from an array using PHP?

我想知道如何使用PHP從數組中刪除空格?

你可以結合使用

碼:

array_filter(array_map('trim', $array));

這將從側面刪除所有空格(但不刪除字符之間)。 並且它將刪除任何等於FALSE輸入條目(例如0,0.00,null,false,...)

例:

$array = array(' foo ', 'bar ', ' baz', '    ', '', 'foo bar');
$array = array_filter(array_map('trim', $array));
print_r($array);

// Output
Array
(
    [0] => foo
    [1] => bar
    [2] => baz
    [5] => foo bar
)

你的問題不是很清楚,所以我會嘗試覆蓋幾乎所有情況。

通常,您需要創建一個能夠執行所需操作的函數,無論是從每個元素的左側和右側移除空格還是完全刪除空白字符。 這是如何做:

<?php

function stripper($element)
{
    return trim($element); // this will remove the whitespace
                           // from the beginning and the end
                           // of the element
}

$myarray = array(" apple", "orange ", " banana ");
$stripped = array_map("stripper", $myarray);
var_dump($stripped);

?>
Result:

Array
(
    [0] => "apple"
    [1] => "orange"
    [2] => "banana"
)

你可以從這里拿走它。

$subject = $_REQUEST['jform']['name_cat'];
$input = str_replace(" ","",$subject);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM