Forum Moderators: coopster

Message Too Old, No Replies

how do I set a variable to a string that contains doublequotes?

         

ocelot

11:59 pm on Dec 5, 2004 (gmt 0)

10+ Year Member



I want to put a bunch of html code inside a php variable and then echo it later. but the html code contains doublequotes ("), which breaks the variable assignment.

I know I could escape every single one with a backslash, but that just seems really clumbsy to me. Isn't there a way to just make php take the string verbatim?

baze22

12:05 am on Dec 6, 2004 (gmt 0)

10+ Year Member



2 options off the top of my head depending on how you are using the string.

1. Try addslashes() and stripslashes().

2. Use single quotes around the string. (this is probably what you are looking for)

$s = '<td align="center">';

baze

bubone2

2:57 am on Dec 6, 2004 (gmt 0)

10+ Year Member



My suggestion is along the lines of delimiting each of the doublequotes. But automate it by using string replace method:

$string = str_replace('"', '\"', $string);

[for clarification, that is ... (singlequote doublequote singlequote, single \double single, $string)]

Now I haven't actually tested this little bit of code, but I know that the function itself is very handy. The part I am not sure about is using the singlequotes around the doublequote ('"'), but I am pretty sure that will work nicely.

One thing that I have used this for in the past is for inputting names into a database. I had a problem with names that had apostrophes in them (like O'Neil). I used the following which is very similar:

$LAST_NAME = str_replace("'", "\'", $LAST_NAME);

If you ever need to change it back, just follow the same methodology to restore the string. I believe that this will produce the same result as the adddslashes function, I just haven't used the addslashes function b4. In any event, they are both one line of code. Its more of a programming style than anything there, so take your pick.

Hope that helps.

Josh