Pregunta

I'm using PHP environment and I have a problem in displaying data on form of escape characters.

example:

how could i display a double quote " if my string data is \"

same with, how could i display a backslash \ if my string data is \\

¿Fue útil?

Solución

To remove \ characters that are used as an escape characters, use the stripslashes function.

david@raston ~ $ cat tmp/test.php
<?php

$raw = 'double slash \\\\ escaped quote \"';
print $raw;
print "\n";
print stripslashes($raw);
?>

david@raston ~ $ php tmp/test.php
double slash \\ escaped quote \"
double slash \ escaped quote "

Otros consejos

Everyone else has covered it and this is all documented here but for the sake of completeness I'd also suggest taking a look at the heredoc notation which can be useful when your data is changing through several contexts (eg. PHP -> HTML/JS -> JS-Regex)

<?php
echo <<<__EOF
It's not great for PHP indentation but often helps readability \ " ' `
__EOF;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top