-3

Possible Duplicate:
How do I strip all spaces out of a string in PHP?

Simply as it should be:

$text = 'This is the text with 3445';

I want it to be:

$trimmed = 'Thisisthetextwith3445';
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Khaled Mahmoud
  • 302
  • 1
  • 7
  • 16
  • 5
    Please search before posting or look at the similar questions list while you are writing your question. – Evan Mulawski Mar 21 '12 at 12:03
  • try `str_replace(' ','',$text);` or go for regular expression. – Sumant Mar 21 '12 at 12:07
  • @EvanMulawski I searched, maybe there are similar questions but I didn't find what I was looking for. PLUS: the question is too short and direct. You could 've written (str_replace(' ','',$text);) instead. – Khaled Mahmoud Mar 21 '12 at 12:07
  • 1
    @KhaledMahmoud: Google "php remove all whitespace from string" - because that's how I found the duplicate - and in about two seconds. – Evan Mulawski Mar 21 '12 at 12:24
  • @KhaledMahmoud Laziness isn't often rewarded on stackoverflow.. no matter the degree. – Mike B Mar 21 '12 at 12:29
  • @EvanMulawski I didn't ask my question for people search the answer!! I asked so that anyone who knows the answer can write it down :) stackoverflow is made for people to help each other with what they already know, not to search the answer for others. – Khaled Mahmoud Mar 21 '12 at 12:36
  • Anyway,, thanks for those who answered. I'll search better next time. – Khaled Mahmoud Mar 21 '12 at 12:41
  • @KhaledMahmoud: That's not how it works. Before posting a question, it is your duty to search for an answer first (an in your case there was an *exact* answer). Why else do we close duplicate questions? – Evan Mulawski Mar 21 '12 at 12:48

3 Answers3

3

You could do:

str_replace(' ', '', $text);

as stated in How do I strip all spaces out of a string in PHP? which you could have found easily.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shikiryu
  • 10,180
  • 8
  • 49
  • 75
  • Maybe because answering a question that you know for a fact is a duplicate can be considered point-whoring. I didn't downvote, but if I noticed the question was a dupe I would have deleted my answer and voted to close. – Mike B Mar 21 '12 at 13:08
  • I don't mind not having upvote for a dup'. I answered before knowing it was one, edited it then, voted for close (as you can see) but won't delete my answer for that. Downvotes are for bad answers, if anyone thinks I'm point-whoring, don't vote at all. Plus, it's nice to give a comment when you downvote, if you follow the netiquette that is. – Shikiryu Mar 21 '12 at 13:25
  • No, downvotes are for answers that aren't useful. You pointed out this question has been asked and adequately answered before.. making your own answer a duplicate and thus not useful. – Mike B Mar 21 '12 at 13:27
  • That is correct, anyway, according to [the vote-down page](http://stackoverflow.com/privileges/vote-down) : **Downvoting should be reserved for extreme cases.** I don't think this is one ;-) I just think *this* downvoter is a "rager" who doesn't comment his downvote. You did it for him, he should be thankfull. I'll do it for him : thanks :) – Shikiryu Mar 21 '12 at 13:33
3
$trimmed = str_replace(' ', '', $text);
echo $trimmed;
martincarlin87
  • 10,848
  • 24
  • 98
  • 145
1

To remove one or more occurrences of white spaces:

$foo = preg_replace('/\s+/', ' ', $foo);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tmjam
  • 1,029
  • 2
  • 12
  • 25