Jump to content


Photo

Removing excess spaces?


  • Please log in to reply
3 replies to this topic

#1 zeroblitzt

zeroblitzt
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 04 August 2006 - 10:51 PM

Hey all, i'm a total regex beginner (never used it), but someone recommended that I use it. What I need to do with it is remove excess spaces from a string (for my message boards).

So if I were to have, say:
[pre]Hello    Everyone[/pre]

it would shorten it down to
[pre]Hello Everyone[/pre]

I'm not sure how to go about doing it though. Any tips?

#2 xec

xec
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 05 August 2006 - 06:53 AM

its very easy ,

(\040)+  -- this is the meta character represent space.. and + sign represent if you want to remove multiple spaces. ;)

#3 boby

boby
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 05 August 2006 - 02:21 PM

You can use this little preg_replace:
<?php
$string = trim (preg_replace ('`[\s]+`', ' ', $string));
?>

Hope it helps ;)
Boby

#4 zeroblitzt

zeroblitzt
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 05 August 2006 - 10:42 PM

Wow thanks for the help! I'll put you both in the credits of my source code  :D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users