Page 1 of 1
putting text with alot of " ' into a variable...
Posted: Sun Jul 20, 2003 6:50 am
by Fredix
Hi,
I have another problem:
how can I put some text into a variable that contains alot of " and ' that should not be escaped by a \. I know in PERL you can write something like:
$text = |
""''ladsjflaksjdfh""''
|;
is there something similar in PHP?
Posted: Sun Jul 20, 2003 7:46 am
by Drachlen
$text = stripslashes($text);

Posted: Sun Jul 20, 2003 8:45 am
by Gen-ik
You need to create your string with escape slashes in it... $text="I said \"NO\" didn't i?" ...... and then when you want to display the string on a page use ..... echo(stripslashes($text)) ......
Posted: Sun Jul 20, 2003 11:29 am
by Stoker
Escapes are not literals, stripslahses is intended to remove literal escapes (such as those inserted autopmatically by magic-quotes-gpc)...
To generate a string with quotes in it, you MUST escape these for PHP, the escapes will not become a part of the literal string they just simply tells PHP that it is a literal ' or "
So this should be no problem at all, if you are hardcoding the strings you can easily add the escapes, if you are getting the data from anywhere else there is no need to do anything if the string contains the special chars.. If the string does in fact contain escapes (after e.g. a post) you need to use stripslashes() to clean it up..
Posted: Sun Jul 20, 2003 6:28 pm
by patrikG
Alternatively, you can convert quotes to their HTML-ASCII equivalents using
htmlspecialchars().
'&' (ampersand) becomes '&'
'"' (double quote) becomes '"' when ENT_NOQUOTES is not set.
''' (single quote) becomes ''' only when ENT_QUOTES is set.
'<' (less than) becomes '<'
'>' (greater than) becomes '>'
That way you do not have to escape the quotes. However, this is only useful if you mean to output the data. If you want to write it into the DB and process it later PHP-internally, you'd probably have to parse the replaced values again.
Posted: Sun Jul 20, 2003 6:53 pm
by Gen-ik
patrikG wrote:
That way you do not have to escape the quotes. However, this is only useful if you mean to output the data. If you want to write it into the DB and process it later PHP-internally, you'd probably have to parse the replaced values again.
Yep that's correct... unfortunatly. htmlspecialchars() is good but it's only a one way thing so you would need to be careful how you use it in your code.
As far as I can remember I haven't used it at all yet so there are a lot of work-arounds or easier ways of doing this kind of thing.. even if it does mean a few more lines of code.
Posted: Sun Jul 20, 2003 9:32 pm
by qartis
Though you may just want to use
Code: Select all
$var = <<<ABC
this is a really big string with 'quotes' and "double" quotes.
ABC;
echo $var;
Posted: Mon Jul 21, 2003 4:17 am
by twigletmac
Here Doc format would be my choice (like the example qartis gave):
http://www.php.net/manual/en/language.t ... ax.heredoc
Mac
Posted: Mon Jul 21, 2003 12:33 pm
by Fredix
thank you.
the here doc was especially interesting for me!