我想知道如何使用PHP从数组中删除空格?
You can use a combination of
array_filter
— Filters elements of an array using a callback function array_map
— Applies the callback to the elements of the given arrays trim
— Strip whitespace (or other characters) from the beginning and end of a string Code:
array_filter(array_map('trim', $array));
This will remove all whitespace from the sides (but not between chars). And it will remove any entries of input equal to FALSE (eg 0, 0.00, null, false, …)
Example:
$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
)
Your question isn't very clear, so I will try to cover almost all cases.
In general, you need to create a function which will do what you need, be it removing the spaces from the left and right of each element or remove the whitespace characters completely. Here's how:
<?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" )
You can take it from here.
$subject = $_REQUEST['jform']['name_cat'];
$input = str_replace(" ","",$subject);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.