Forum Moderators: coopster

Message Too Old, No Replies

stripping spaces and line breaks

what php function should I use?

         

Anguz

5:15 am on Aug 1, 2003 (gmt 0)

10+ Year Member



I have a condition where the variable I'm checking has to be either empty or not, but sometimes it has a space or a new line

those that are just spaces or new lines should count as empty, but they don't, so just for the condition, I need to get rid of them

what function(s) should I use for this?

thank you very much in advance! :)

brotherhood of LAN

6:39 am on Aug 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could use preg_replace

$yourvar = preg_replace("'[\n\r\s\t]'","",$yourvar);

if(empty($yourvar))
{......

That'll replace new lines, carriage returns spaces and tabs. A few other PHP string functions should do the same trick.

Anguz

6:57 am on Aug 1, 2003 (gmt 0)

10+ Year Member



thank you! I'll try it :)

I've been reading the manual and found that maybe trim($yourvar) could work too, would it?

brotherhood of LAN

7:05 am on Aug 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



yep, trim() will do it, but only if they are at the beginning or end of your string.

Any in the middle it would not strip. If you only need to strip them from beginning or end I'd use trim(), it's probably more efficient code ;)

Anguz

4:14 pm on Aug 1, 2003 (gmt 0)

10+ Year Member



well, if the whole string in only spaces or \n or \r or \t, there's nothing in the middle, like:

"

"

in that case trim() should take away everything

thank you very much for your help :)