Home >Backend Development >PHP Tutorial >PHP method to clear spaces at both ends of all strings in an array, array string_PHP tutorial

PHP method to clear spaces at both ends of all strings in an array, array string_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:16:331090browse

PHP method to clear spaces at both ends of all strings in an array, array string

The example in this article describes the method of clearing spaces at both ends of all strings in an array in PHP. I share it with you for your reference. The specific implementation method is as follows:

Generally speaking, there are many ways to clear spaces in strings in PHP, but we cannot simply use these methods to clear the codes before and after all values ​​in the array. The example in this article mainly uses PHP's unique array_map function to traverse Removes whitespace from both ends of all strings in the array.

The specific implementation code is as follows:

Copy code The code is as follows:
function TrimArray($Input){
If (!is_array($Input))
         return trim($Input);
Return array_map('TrimArray', $Input);
}
/*
Old version (v0.1): The old version is for everyone’s comparison reference:
function TrimArray($arr){
If (!is_array($arr)){ return $arr; }
While (list($key, $value) = each($arr)){
If (is_array($value)){
               $arr[$key] = TrimArray($value);
}
         else {
               $arr[$key] = trim($value);
}
}
Return $arr;
}
*/

//Demo example:
$DirtyArray = array(
'Key1' => ' Value 1 ',
'Key2' => ' Value 2 ',
'Key3' => array(
‘ Child Array Item 1 ‘,
          ' Child Array Item 2'
)
);
$CleanArray = TrimArray($DirtyArray);
var_dump($CleanArray);

Result will be:
array(3) {
["Key1"]=>
string(7) "Value 1"
["Key2"]=>
string(7) "Value 2"
["Key3"]=>
array(2) {
[0]=>
String(18) "Child Array Item 1"
[1]=>
String(18) "Child Array Item 2"
}
}

I hope this article will be helpful to everyone’s PHP programming design.

How to use php program to delete "spaces" in "string elements" in "array"?

$arr=array();
$arr[]="ad dfd dfd";
$arr[]="saf sdf dsf";
$arr[ ]="sdf dsfgfd dd";
$arr[]="dfd dfferw ";
while(list($name,$value)=each($arr)){
echo $value;
$arr2[]=trim($value);//Remove spaces
}
print_r($arr2);//This should be the array you want~
?>

How to remove extra spaces from a string array

This algorithm actually just moves each space backward. The problem is that it gets awkward when two spaces are juxtaposed...
It can be slightly modified so that it does not move with the next space every time it encounters a space. Instead of exchanging one character, search for the next non-space character and exchange it with it, and finally remove all the extra spaces, so it’s OK

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/897016.htmlTechArticlePHP method to clear the spaces at both ends of all strings in the array, array string This article tells the example of PHP clearing the spaces in the array All methods of using spaces at both ends of strings are shared with you for your reference. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn