Current Location: Home> Latest Articles> How to Use PHP's str_split Function with array_filter to Remove Whitespace Characters from a String

How to Use PHP's str_split Function with array_filter to Remove Whitespace Characters from a String

M66 2025-06-15

String manipulation is a common task in PHP, and removing whitespace characters from a string is one of the tasks many developers need to handle. Typically, whitespace characters include spaces, tabs (Tab), newlines, etc. This article will show you how to efficiently remove whitespace characters from a string using PHP's str_split function combined with array_filter function.

1. Introduction to str_split Function

str_split function splits a string into an array of individual characters. The basic usage of this function is as follows:

str_split(string $string, int $split_length = 1): array
  • $string: The string to be split.

  • $split_length: The length of each array element, default is 1.

For example, let's say we have a string Hello World, calling str_split will give the following result:

$string = "Hello World";
$array = str_split($string);
print_r($array);

Output:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>  
    [6] => W
    [7] => o
    [8] => r
    [9] => l
    [10] => d
)

As you can see, the space character is also included as a separate element in the array.

2. Introduction to array_filter Function

array_filter function is used to filter elements in an array based on a callback function. The basic usage is as follows:

array_filter(array $array, callable $callback, int $flag = 0): array
  • $array: The input array.

  • $callback: The callback function that tests each element in the array. If the callback function returns true, the element will be kept; if it returns false, the element will be removed.

  • $flag: Controls how the array keys are handled, default is 0.

For example, the following code demonstrates how to remove all empty values from an array:

$array = ['a', '', 'b', null, 'c'];
$result = array_filter($array);
print_r($result);

Output:

Array
(
    [0] => a
    [2] => b
    [4] => c
)

3. Removing Whitespace Characters Using str_split and array_filter

By combining str_split and array_filter, we can easily remove all whitespace characters from a string. First, we split the string into a character array using str_split, then use array_filter to remove all whitespace characters.

Here is an example code:

<?php
$string = "  H e l lo  Wo r ld  ";
<p>// Split the string into an array using str_split<br>
$array = str_split($string);</p>
<p>// Remove whitespace characters using array_filter<br>
$result = array_filter($array, function($char) {<br>
return !ctype_space($char);  // Filter out whitespace characters<br>
});</p>
<p>// Recombine the result array into a string<br>
$filtered_string = implode('', $result);</p>
<p>echo $filtered_string;  // Output "HelloWorld"<br>
?><br>

In this example:

  • str_split splits the string into individual characters.

  • array_filter uses the callback function ctype_space() to check whether each character is a whitespace character (space, tab, newline, etc.). ctype_space() is a PHP function that checks if a character is a whitespace character.

  • Finally, we use implode to recombine the filtered character array into a string, resulting in the string without any whitespace characters.

Running the above code will output:

HelloWorld

4. Conclusion

By combining str_split and array_filter, you can easily remove all whitespace characters from a string. This method is simple and efficient, especially when performing character-level operations on a string. Using this method ensures that there are no extra spaces, newlines, or tabs in the string you are processing.

We hope this article helps you understand how to use PHP's str_split and array_filter functions to handle whitespace characters in strings.