0

Try to remove these whitespace array form this

Im also trying to remove whitespace with preg_replace, explode, trim

`Array
(
[0] => s
[1] => 
[2] => s
[3] => 
[4] => a
[5] => 
[6] => i
[7] => 
[8] => 2
[9] => 
[10] => 2
)`
3
  • 1
    Please show what you have tried. Commented Apr 28, 2018 at 15:17
  • $newmail=preg_replace('/\s+/', '', $email); Commented Apr 28, 2018 at 15:20
  • $charSet = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $email); $charSet = rtrim($charSet); $charSetArray = explode(" ", $charSet); Commented Apr 28, 2018 at 15:22

1 Answer 1

1

You can use array_filter to remove empty array elements

$arr = array('s','','s','','a','i','',2,'',2);

$arr = array_filter($arr);

echo "<pre>";
print_r( $arr );
echo "</pre>";

This will result to:

Array
(
    [0] => s
    [2] => s
    [4] => a
    [5] => i
    [7] => 2
    [9] => 2
)

Or you can use trim as callback if there are multiples spaces. Like:

$arr = array('s','   ','s','    ','a','i','   ',2,'',2);

$arr = array_filter($arr,'trim');

echo "<pre>";
print_r( $arr );
echo "</pre>";

Will get the same result.

Doc: array_filter

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.