How to trim white spaces of array values in php
array_map and trim can do the job
$trimmed_array = array_map('trim', $fruit);
print_r($trimmed_array);
Remove white space in the array elements with trim not works
trim()
only removes whitespace from the beginning and the end. You probably want
array_map(function($a){
return str_replace(' ', '', $a);
}, $myarray);
Remove whitespace from beginning of each array element
Ok to ltrim()
, but use it with array_map()
:
$lines = array_map( 'ltrim', $liness );
file_put_contents( 'file.txt', implode($lines) );
And, don't use echo
for an array, use print_r()
instead. Or:
foreach( $lines as $line ) echo "$line\n";
- Read more about array_map()
Remove spaces from array values
Simply use array_map
, array_filter
and explode
like as
$string = '1, 2,3,';
print_r(array_map('trim',array_filter(explode(',',$string))));
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Explanation:
Firstly I've split string into an array using explode
function of PHP
print_r(explode(',',$string));
which results into
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] =>
)
So we need to remove those null values using array_filter
like as
print_r(array_filter(explode(',',$string)));
which results into
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Now the final part need to remove that (extra space)
from the values using array_map
along with trim
print_r(array_map('trim',array_filter(explode(',',$string))));
SO finally we have achieved the part what we're seeking for i.e.
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Demo
How to remove white space in array values in PHP
Below loop may be helpful for us.
foreach($array as &$val){
$data[] = str_replace(' ','',$val);
}
Thanks
How to remove array elements that are containing only white spaces?
You could just couple it with array_map
using trim
:
$arr = array_filter(array_map('trim', $arr));
This doesn't reindex the keys though, if you want you could just use array_values
:
$arr = array_values(array_filter(array_map('trim', $arr)));
PHP fails to remove white spaces from a arrays
If you are expecting characters and numbers you can use this(keeping *
in the string):
preg_replace("/[^a-zA-Z0-9\*]/", "", $td->nodeValue);// this will remove spaces from a proper string also.
or
$data[$row][] = preg_replace("/[^ \w\*]+/", "", $td->nodeValue);// this works better and does not remove spaces
How can I trim all strings in an Array?
array_map() is what you need:
$result = array_map('trim', $source_array);
How to trim the whitespace from an array in PHP
You are not modifying the original array when you do the trim. You should get the values by reference in the foreach
loop.
foreach($csv as &$row){
foreach($row as &$cell){
$cell = trim($cell);
}
}
From the documentation:
In order to be able to directly modify array elements within the loop
precede $value with &. In that case the value will be assigned by
reference.
How can I remove the last empty space from my string inside array?
You're missing to assign the result of rtrim ($str, "\n");
. So your variable $str
is not changed.
$str = rtrim($str, "\n");
Or, by default, to remove all whitespace characters (" \t\n\r\0\x0B"
):
$str = rtrim($str);
Related Topics
PHP Syntax for Arrays: Different Behaviour Between PHP Versions
How to Access a Deep Object Property Named as a Variable (Dot Notation) in PHP
What Does Double Question Mark () Operator Mean in PHP
How to Disable Redirection After Login_Check in Symfony 2
What Is the Equivalent of JavaScript's Encodeuricomponent in PHP
Best Way to Process Large Xml in PHP
PHP Warning Permission Denied (13) on Session_Start()
Subquery in Doctrine2 Notin Function
How to Replace a Function in PHP (Such as Mail) and Make It Do Something Else
How to Get Common Values from Two Different Arrays in PHP
Swift Httppost Data Not Inserting to MySQL Database
Mysql_Num_Rows() Expects Parameter 1 to Be Resource, Boolean Given In
How to Trim White Spaces of Array Values in PHP
How to Password Protect Folder/Page Using PHP Without a Db or Username
Does Utc Observe Daylight Saving Time