Dan Nite
Dan Nite

Reputation: 79

PHP How to trim whitespaces a from string

I want to know how to replace whitespaces from a String.

Example String: INFO [06:57:18 INFO]: 03.06 07:05:32 Auto-saving 03.06 07:05:32 [Console] INFO CONSOLE: Enabled level saving 03.06 07:05:32 [Console] INFO CONSOLE

I want to make it: INFO [06:57:18 INFO]: 03.06 07:05:32 Auto-saving 03.06 07:05:32 [Console] INFO CONSOLE: Enabled level saving 03.06 07:05:32 [Console] INFO CONSOLE

I tried the following methods which didn't work for me:

preg_replace('/\s+/', '', $foo);
trim($foo);

I tried to print it as json to see what is really going on and got this output: http://pastebin.com/QY8uGt4V (the output is pretty big)

Upvotes: 0

Views: 64

Answers (3)

Rahil Wazir
Rahil Wazir

Reputation: 10142

Try this:

preg_replace('/[\b]+/', '', $str);

Upvotes: 2

SERPRO
SERPRO

Reputation: 10067

What you want to do is replace multiple spaces with one? if So you just need to set a space character as the replacement, like this:

preg_replace('/\s+/', ' ', $foo);
trim($foo);

Upvotes: 0

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

Try this

$str = 'INFO                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          [06:57:18 INFO]: 03.06 07:05:32 Auto-saving 03.06 07:05:32 [Console] INFO CONSOLE: Enabled level saving 03.06 07:05:32 [Console] INFO CONSOLE;';
$output = preg_replace('!\s+!', ' ', $str);
echo $output;

Upvotes: 0

Related Questions