views:

37

answers:

3

How do you strip the first (before any visible text) enter/return space from text taken from a variable (submitted from a textarea)?

+1  A: 

Like this:

$str = str_replace("\n\r", '', $content);

Note: If you want to convert newliness to <br />, use nl2br instead.

Note also that "\n\r" is for Windows systems, it is \n on unix/linux.

Update:

Try this regex:

$str = preg_replace(/[\n\r{2,}]+/, "\n", $content);
Sarfraz
sorry - meant to say the first whitespace.. for example, when user accidentally presses enter a few times before actual visible text entry
ina
@ina: See my updated answer please.
Sarfraz
+1  A: 
$userinput = trim($userinput)

trim() works for followings, though you can strip some of those giving a char list
    * " " (ASCII 32 (0x20)), an ordinary space.
    * "\t" (ASCII 9 (0x09)), a tab.
    * "\n" (ASCII 10 (0x0A)), a new line (line feed).
    * "\r" (ASCII 13 (0x0D)), a carriage return.
    * "\0" (ASCII 0 (0x00)), the NUL-byte.
    * "\x0B" (ASCII 11 (0x0B)), a vertical tab.
Sadat
trim does not work for enter spaces...
ina
i think it does- http://php.net/manual/en/function.trim.php
Sadat
+1  A: 

ltrim removes only whitespaces before the text. you can put an array of additional chars you dont need into the second argument.

$userinput = ltrim($userinput, $charsYouDontNeed);

the fact that it doesn't work might mean that the chars you are trying to delete are not really new line chars

kgb