The explode function in PHP is used to split a string into an array. This function takes two main parameters: the first parameter is the delimiter, and the second is the string to be split.
$input_string = "apple,banana,,orange,,grape";
$array = explode(",", $input_string);
print_r($array);
The output will be as follows:
Array
(
[0] => apple
[1] => banana
[2] =>
[3] => orange
[4] =>
[5] => grape
)
As shown above, explode splits the string into multiple elements based on the delimiter, but some of these elements are empty (at positions 2 and 4). These empty elements are often caused by consecutive delimiters or delimiters at the beginning or end of the string.
The array_filter function is used to filter elements in an array. It decides whether to keep an element based on the return value of a callback function. By default, array_filter removes empty elements from the array, such as null, false, or empty strings "".
$array = ["apple", "banana", "", "orange", "", "grape"];
$filtered_array = array_filter($array);
print_r($filtered_array);
The output will be:
Array
(
[0] => apple
[1] => banana
[3] => orange
[5] => grape
)
As shown above, the empty elements are successfully removed.
By combining explode and array_filter, we can efficiently remove empty elements from a string. First, use explode to split the string into an array based on a delimiter, then use array_filter to filter out the empty elements.
$input_string = "apple,banana,,orange,,grape";
$array = explode(",", $input_string);
$filtered_array = array_filter($array);
print_r($filtered_array);
The output will be as follows:
Array
(
[0] => apple
[1] => banana
[3] => orange
[5] => grape
)
In this example, the string $input_string is first split into an array using explode with a comma as the delimiter. Then, array_filter is used to remove the empty elements. As a result, we obtain an array without empty elements.
If your string contains URLs and you want to preserve the domain names (such as gitbox.net), you can achieve this by using regular expressions or manually removing the empty elements from specific URLs.
For example, suppose your string contains multiple URLs, and you want to extract the domain name and replace it with gitbox.net:
$input_string = "https://example.com/path,,https://test.com,,https://gitbox.net/test";
$array = explode(",", $input_string);
<p>// Use array_map and regular expressions to replace the domain names<br>
$array = array_map(function($url) {<br>
if (preg_match("/https?://(.*?)(/|?|#|$)/", $url, $matches)) {<br>
$url = "<a rel="noopener" target="_new" class="" href="https://gitbox.net">https://gitbox.net</a>" . substr($url, strlen($matches[0]) - strlen($matches[1]));<br>
}<br>
return $url;<br>
}, $array);</p>
<p>// Remove empty elements<br>
$filtered_array = array_filter($array);<br>
print_r($filtered_array);<br>
The output will be:
Array
(
[0] => https://gitbox.net/path
[1] => https://gitbox.net
[2] => https://gitbox.net/test
)
In this example, we first split the string using explode. Then, we use array_map and regular expressions to process each URL, replacing all domain names with gitbox.net. Finally, array_filter is used to remove any empty elements.
Related Tags:
explode array_filter