$str = str_replace('\r', '', $str); //清除回车符
$str = str_replace('\n', '', $str); //清除换行符
$str = str_replace('\n', '', $str); //清除换行符
解决方案 »
- 求一个xhtml+css+js+ajax+php+smarty+mysql+MVC的项目视频教程
- 关于smarty数组的一个问题,求指点
- 各位前辈指教
- 又遇到难题。。请教高手
- php源码插入js代码在安装了xp3系统之后不能调用
- 手头有工程,找几个能在空闲时间一起开发程序的主要是使用php+mysql+apache开发
- 注意:postgresql-8.0-beta1已经发布,支持win32平台
- 这段代码是什么意思?
- php4的session问题,紧急!!!
- ZendStudio 10 怎么支持php5.5和php5.6
- 用正则表达式取出一段字符串。
- 紧急求助:错误 MySQL 返回:Too many connections,给分100,在线等待,多谢大家!
用trim()就行了
$s = "abcd
";
echo trim($s);
echo ereg_replace("\r?\n$","",$s)."|";
?>
这两种方式都可以
trim()怎么用?
Strip whitespace from the beginning and end of a string (PHP 3, PHP 4 >= 4.0.0)
string trim ( string str [, string charlist] )Note:
The optional charlist parameter was added in PHP 4.1.0 This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, trim() will strip these characters: " " (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.
You can also specify the characters you want to strip, by means of the charlist parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters. 当字符串前后出现上述的字符时,用Trim()也是可以的。不过如果字符串里面也有回车、换行的话,还是要用我上面的替换方法。
$str = 'dfsdfdsf</script>';
$patterns = "/.*\<\/[script]*\>$/";
print preg_replace($patterns, '', $str);
//print 'skfshksdhf';
?>