putting text with alot of " ' into a variable...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
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...

Post 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?
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post by Drachlen »

$text = stripslashes($text);

:D
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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)) ......
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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..
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post 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;
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

Post by Fredix »

thank you.
the here doc was especially interesting for me!
Post Reply