2

I want to replace multiple spaces for a single space in a string. please advise on how to do it. Example code:

  <?php
    $input="bikash&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ranjan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nayak";
    echo $output =preg_replace('/(( )+|(\\n)+)/', '$2$3',$input);

    ?>

the output is coming :
"bikash     ranjan          nayak"

Joum
  • 3,189
  • 3
  • 33
  • 64
Bikash Nayak
  • 187
  • 1
  • 2
  • 12

7 Answers7

4

You could use a regular expression

$output = preg_replace('!\s+!', ' ', $input);
Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41
  • it`s not working rajeev you can check, i also trying lot of things using preg_replace() fuction – Bikash Nayak Jul 05 '13 at 11:38
  • it won't work in OP's `$input` content – Marcin Orlowski Jul 05 '13 at 11:38
  • reason behind    – Bikash Nayak Jul 05 '13 at 11:41
  • can give for example rajeev – Bikash Nayak Jul 05 '13 at 11:42
  • 1
    Replicating answers on duplicate questions surely is a fun way to harvest rep, isn't it? If it's a duplicate, flag it as one... – Joum Jul 05 '13 at 11:42
  • Yeah, but at least SO doen't get cluttered with duplicate information. Plus, if you are only in it for the rep, _you_ can also flag the question as a duplicate and also _you_ can get rep from it, all this while making SO a better community, get it? – Joum Jul 05 '13 at 12:02
  • Are you then saying that you only do good for the community while there is rep involved? If you can't win rep with it, you contribute to trashing the site up? That's really great! – Joum Jul 05 '13 at 12:31
  • @RajeevRanjan Good for you, bad for the community... – Joum Jul 05 '13 at 13:26
  • @Joum you alone do not represent community. – Rajeev Ranjan Jul 05 '13 at 13:34
  • @RajeevRanjan You are correct. But I play fair and by the rules of SO. a) You blatantly "stole" an answer from a duplicate question and posted here as a reply. b) You admit to not flagging it because you want to earn rep from it and are out of flag votes, so instead of crediting the people that actually did something inovative about this question, you copy from them and take their credit. c) this really bad attitude from your behalf only makes people that use the site and colaborate in a fair way become distrusting of you. But hey, it is your option - and you don't _care_ what _I_ think. – Joum Jul 05 '13 at 13:41
  • FYI: http://meta.stackexchange.com/questions/78658/is-it-okay-to-copy-paste-answers-from-other-questions – Joum Jul 05 '13 at 13:49
2

Try this one. It will display as a single space on browser

$output = str_replace("&nbsp", " ",$input);
Nabeel Arshad
  • 467
  • 2
  • 7
  • 22
  • ok thanks lastly resolved my replace a string . before that i was missing some thing but same thing i used not worked. thanks again – Bikash Nayak Jul 05 '13 at 11:51
1

Try this

$output = implode("&nbsp;",array_filter(explode("&nbsp;",$input)));
Nauphal
  • 6,194
  • 4
  • 27
  • 43
1
$output = preg_replace('!\(&nbsp;)+!', '&nbsp;', $input);
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
1

try this

$output = preg_replace('/\s+/', ' ',$input);
nickle
  • 4,636
  • 1
  • 13
  • 11
1

Added the extra line ($input = html_ent.....) which decodes the html entitiy's.

$input="bikash&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ranjan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nayak";

$input = html_entity_decode($input);

echo $output =preg_replace('/(( )+|(\\n)+)/', '$2$3',$input);
Benz
  • 2,317
  • 12
  • 19
1

Try this code

$input="bikash&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ranjan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nayak";
$array = explode('&nbsp;', $input);
$output = implode('&nbsp;', array_filter($array));
echo $output;

One liner:

$output = implode('&nbsp;', array_filter(explode('&nbsp;', $input)));
Bram Verstraten
  • 1,414
  • 11
  • 24
  • you r right but code is too long "work good" – Bikash Nayak Jul 05 '13 at 11:55
  • I can write it as one line if you want. The solution you selected is not correct. It will still put down multiple spaces, but the browser will show only 1 or 2. And the spaces are not "non breaking spaces". – Bram Verstraten Jul 05 '13 at 11:57