Home >Backend Development >PHP Tutorial >PHP method to clear spaces at both ends of all strings in an array, array string_PHP tutorial
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:
I hope this article will be helpful to everyone’s PHP programming design.
$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~
?>
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