Frage

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 \\

War es hilfreich?

Lösung

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 "

Andere Tipps

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;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top