Remove white space from csv

Asked

Viewed 406 times

0

I’m having a problem when the user climbs up a .CSV. file If in some of the fields there is a blank space at the end I can’t remove it in any way.

For example: "[email protected] "

I’ve tried to use trim(), rtrim() and it doesn’t work When I use str_replace() it removes all the spaces you have in the middle of the string but the one at the end does not remove.

I have looked everywhere and can’t find a solution. If anyone can help me, I would appreciate.

Code as requested:

foreach($column_headers as $key => $value)
{
     $result[$new_row][$value] = trim($data[$key]);
}
  • Put the code snippet, it’s easier to understand the problem.

  • @Marcosmarques edited the question with the code as requested. Thanks

  • Have you considered the ascii table in str_replace?

1 answer

2


the only way I could get that blank space out was like this:

$email = preg_replace('/[[:^print:]]/', '', $email);

from what I’ve seen, that '/[[:^print:]]/' removes non-printable characters.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.