By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,627 Members | 2,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,627 IT Pros & Developers. It's quick & easy.

How do I remove extra white space in a string?

P: 27
I am currently getting some forecast data from a web site using (file_get_contents), then I'm using explode to put the string in an array, which I am then parsing.

The trouble is when I use explode() are several empty elements between each full one.

Example.
Array ( [0] => this [1] => is [2] => [3] => [4] => a [5] => [6] => [7] => test )

If I print the sting before I explode it I only see one space between each word, but the it seems to be that there is more than one.

Is there a nice php function that will remove all but one white space or does someone have a nice function written which they could show me how to do this.

I understand I can use trim() on the ends but what about extra white space between the words?

thanks
Mar 24 '08 #1
Share this Question
Share on Google+
1 Reply


Markus
Expert 5K+
P: 6,050
You can use preg_replace to remove whitespace:
[php]
$_subject = "this is a string with whitespace! !";
echo preg_replace('/\s/', ' ', $_subject);
[/php]

Hope this helps :)

Regards
Mar 24 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.