0

我有一个关于filter

我有以下内容:

$htmlData包含用户输入html字符串,就像

$htmlData = array('<em>test</em>', 'text here','\n')

//I use htmlspecialchars function and want to filter \n out and save it to DB.

       $texts = htmlspecialchars($htmlData[$i]);

        if(!empty($texts) && $text != '\n'){
             echo $text;   //I still see '\n' here for some reason.
         }

我不想保存'\n'到数据库,因为它在DB. 有没有办法防止这种情况发生?谢谢您的帮助!

4

2 回答 2

1

使用trim()

此函数返回一个字符串,其中从 str 的开头和结尾去除了空格。如果没有第二个参数, trim() 将删除这些字符:

" " (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

对于其他解决方案,请参阅此问题:Remove new lines from string

于 2013-07-29T23:57:14.287 回答
1

$text != '\n'将只检查字符串是否包含反斜杠,后跟字母“n”。您要与之比较的是换行符,因此您必须改用换行符$text != "\n"

另请参阅http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double

于 2013-07-30T00:09:28.753 回答