View Full Version : [PHP] Trim Function Alternative
thesleuth
October 30th, 2006, 05:32 AM
Hi,
I'm storing a message in a database. I want to prevent the database from saving spaces or lines that are created by users when they hit enter. I've tried the trim function but it does not work. Any suggestion would be helpful. Thanks in advance :)
duncanhall
October 30th, 2006, 06:19 AM
I think you'll find that 'Trim' does indeed work, but it only removes whitespace from the beginning and end of a string.
You need to use the 'str_replace' function and replace any occurences of " " with "".
Voetsjoeba
October 30th, 2006, 07:07 AM
An easy way to accomplish this is by using a preg_replace:
$message = "A nice text with some whitespace in it
Newline here
More newlines
First, you must bring us ... another shrubbery !
Then ! When you have found the shrubbery !
You must cut down the mightiest tree in the forest ... WIIIIIIIITH !!
A HERRING !! <tuduuuhm>
";
$message = preg_replace( '_\n|\s_' , '' , $message );
print $message;
If you don't want the spaces to be filtered out, replace _\n|\s_ with _\n_
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.