putting text with alot of " ' into a variable...
Moderator: General Moderators
- Fredix
- Forum Contributor
- Posts: 101
- Joined: Fri Jul 18, 2003 2:16 pm
- Location: Wehr (Eifel) Germany
- Contact:
putting text with alot of " ' into a variable...
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?
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?
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..
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..
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.
'&' (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.
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.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.
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.
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;- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Here Doc format would be my choice (like the example qartis gave):
http://www.php.net/manual/en/language.t ... ax.heredoc
Mac
http://www.php.net/manual/en/language.t ... ax.heredoc
Mac