Php single and double quotes

//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 returns the HTML email address \ r \ t \ n while 2 returns the HTML email address

what's with quotes?

+5
source share
2 answers

According to PHP Documentation

Unlike double-quoted and heredoc syntax, variables and escape sequences for special characters will not expand if they appear in single quotes.

In other words, double-quoted strings extend variables and escape sequences for special characters. Single quotes do not have.

So, in example1, with a single quote string, the string is exactly the same as you see it. Slash and all.

2, \r\t\n, , , . , escape- .

+11

PHP , \n\r\t... . docs:

, (\). , (\\). : , escape-, , \r \n, , , - .

0

All Articles