//remove line breaks
function safeEmail($string) {
return preg_replace( '((?:\n|\r|\t|%0A|%0D|%08|%09)+)i' , '', $string );
}
/*** example usage 1***/
$from = 'HTML Email\r\t\n';
/*** example usage 2***/
$from = "HTML Email\r\t\n";
if(strlen($from) < 100)
{
$from = safeEmail($from);
echo $from;
}
1 返回 HTML 电子邮件\r\t\n 而 2 返回 HTML 电子邮件
引号是什么?
最佳答案
Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
换句话说,双引号字符串扩展变量和特殊字符的转义序列。单引号字符串不会。
所以在示例 1 中,使用单引号引起来的字符串与您看到的完全一样。斜杠等等。
但在示例 2 中,它不是以字符串 \r\t\n
结尾,而是以回车、制表符和换行结尾。换句话说,特殊字符的转义序列被扩展。
关于php单引号和双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4584203/