0

I have an array where some of its elements are filled with lots of whitespace and \n. I would like to remove this whitespace and \n.

I have been using the following code, but the compiler says that my code does not work for arrays.

//My code that removes white-space    
$input = preg_replace( '/\s+/', ' ', $myArray);

//Sample of array with white space
$myArray = array(
    'extra spaces' => '<div>     </div>',
    'return' => '<b>\n</b>',
)

//What I want the array to look like
$myArray = array(
    'extra spaces' => '<div></div>',
    'return' => '<b></b>',
)
4
  • 4
    You might be interested in PHP's array_walk(): "Applies [a] user-defined callback function to each element of [an] array." A simple foreach loop will probably get you there, too. Commented Oct 17, 2013 at 22:59
  • @showdev not trying to steal your answer, I just submitted it when I saw your comment :\ Commented Oct 17, 2013 at 23:03
  • @JochemKuijpers No worries. Thanks for posting your answer. Commented Oct 17, 2013 at 23:06
  • If I understand correctly your question, this should work: $cleanArray = preg_replace( '/[ \n]/', '', $myArray); Commented Oct 17, 2013 at 23:28

3 Answers 3

1

You can use a foreach loop to get through all the elements in your array:

foreach($myArray as $key => $val)
{
    $myArray[$key]=preg_replace( '/\s+/', ' ', $value);
}

I haven't actually checked the code, and haven't ever been good with regex, but assuming your replace works fine, it should be good.

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

Comments

1

Try this:

// loop through each element and apply the function
foreach($myArray as $key => $value) {
    $myArray[$key] = preg_replace('/\s+/', '', $value);
}

Or make a function that does this and use array_walk

Note that I'm replacing your whitespace by an empty string, not by a single space (like you and other answers did). This way you will completely remove the whitespace, instead of replacing it with a space.

Comments

1
  1. When you call preg_replace, $myArray is not yet defined. Switch the definition of $myArray and the call to preg_replace:

    //Sample of array with white space#
    
    $myArray = array(
        'extra spaces' => '<div>     </div>',
        'return' => '<b>\n</b>',
    )
    
    //My code that removes white-space    
    $input = preg_replace( '/\s+/', ' ', $myArray);
    
  2. If you want '<div> </div>' to become '<div></div>', you shouldn't replace '/\s+/' with ' ', but with ''.

  3. \n is considered a line break (and thus matches '/\s+/') only within double quotes, not within single quotes. Change '<b>\n</b>' to "<b>\n</b>" or fix your regular expression.

3 Comments

My guess is that this was a simple sketch of what he was trying to accomplish, hence the Sample of array with white space and What I want the array to look like
Sweet, I didn't know preg_replace() works on arrays : "If subject is an array, then the search and replace is performed on every entry of subject, and the return value is an array as well." @icu222much What was the exact error message regarding "code does not work for arrays"?
@JochemKuijpers No need to be sorry. It has become quite hard to tell whether the error is in the original source code or within the question. A lot of people don't bother to check whether the source code they post really exhibits the problem that they are facing. I came to the conclusion not to bother either, do my best to help and let the OP decide which part of my answer is useful.

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.