How can I remove all empty values when I explode a string using PHP? [duplicate]
Asked Answered
G

4

40

I was wondering how can I remove all empty values when I explode a string using PHP for example, lets say a user enters ",jay,john,,,bill,glenn,,,"?

Thanks in advance for the help.

Here is part of the code that explodes user submitted values.

$tags = explode(",", $_POST['tag']);
Gnaw answered 7/8, 2010 at 21:53 Comment(0)
H
107

E.g. via array_filter() or by using the PREG_SPLIT_NO_EMPTY option on preg_split()

<?php
// only for testing purposes ...
$_POST['tag'] = ",jay,john,,,bill,glenn,,0,,";

echo "--- version 1: array_filter ----\n";
// note that this also filters "0" out, since (bool)"0" is FALSE in php
// array_filter() called with only one parameter tests each element as a boolean value
// see http://docs.php.net/language.types.type-juggling
$tags = array_filter( explode(",", $_POST['tag']) ); 
var_dump($tags);

echo "--- version 2: array_filter/strlen ----\n";
// this one keeps the "0" element
// array_filter() calls strlen() for each element of the array and tests the result as a boolean value
$tags = array_filter( explode(",", $_POST['tag']), 'strlen' ); 
var_dump($tags);

echo "--- version 3: PREG_SPLIT_NO_EMPTY ----\n";
$tags = preg_split('/,/', $_POST['tag'], -1, PREG_SPLIT_NO_EMPTY);
var_dump($tags);

prints

--- version 1: array_filter ----
array(4) {
  [1]=>
  string(3) "jay"
  [2]=>
  string(4) "john"
  [5]=>
  string(4) "bill"
  [6]=>
  string(5) "glenn"
}
--- version 2: array_filter/strlen ----
array(5) {
  [1]=>
  string(3) "jay"
  [2]=>
  string(4) "john"
  [5]=>
  string(4) "bill"
  [6]=>
  string(5) "glenn"
  [8]=>
  string(1) "0"
}
--- version 3: PREG_SPLIT_NO_EMPTY ----
array(5) {
  [0]=>
  string(3) "jay"
  [1]=>
  string(4) "john"
  [2]=>
  string(4) "bill"
  [3]=>
  string(5) "glenn"
  [4]=>
  string(1) "0"
}
Howlett answered 7/8, 2010 at 21:56 Comment(4)
@Howlett are you showing me 2 examples because $tags = preg_split('/,/', $_POST['tag'], -1, PREG_SPLIT_NO_EMPTY); alone works. Thanks!Gnaw
@snag: Yes, he was showing two different ways to do it. The array_filter method he listed first is probably more standard, but as he mentioned, it could remove values you wanted to keep, so he included the preg_split method as an alternative.Matrilateral
The reason this isn't working for some is because it doesn't handle whitespace. ie.`a,b,c, ,d". For that, I recommend array_filter with a callback.Handicapper
If you want to make sure your array is re-indexed then wrap it in array_valuesValois
V
4
$tags = array_diff(explode(",", $_POST['tag']),array(""));
Valid answered 22/12, 2016 at 7:57 Comment(0)
C
0
//replace multiple commas
$tags = preg_replace('/,+/', ',', $_POST['tag']);
//explode
$tags = explode(',', $tags);
Counterblow answered 7/8, 2010 at 21:58 Comment(2)
Should be explode(',', $tags) and keeps the last empty element for the given string ",jay,john,,,bill,glenn,,,".Howlett
still keeps the last empty element ;-)Howlett
V
-4

Simply try:

$tags = explode(", ", $_POST['tag']);

Adding space after comma, does the job!

Venomous answered 5/8, 2019 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.