How do you strip the fi开发者_如何学编程rst (before any visible text) enter/return space from text taken from a variable (submitted from a textarea)?
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);
$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.
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
Try:
$str = preg_replace('/[\n\r{2,}]+/', "\n", $content);
精彩评论