html to php - dealing with quotes problem

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
avianrand
Forum Newbie
Posts: 3
Joined: Tue Jun 29, 2010 2:05 pm

html to php - dealing with quotes problem

Post by avianrand »

The following works just fine. But I need to add something that has quotes in it.

echo "<p class='verticaltext'>" . $name_char . "</p>";

The problem is that the whole echo statement has to be in double quotes “ and anything inside of that that would normally be in double quotes in HTML has to be in single quotes. Well what about something in that that would be in single quotes?

In regular HTML I would do this:

<p class=”verticaltext” onmouseover=”this.style.color=’red’”>Some Text To Display</p>

The word red is in single quotes.

I need this:

echo "<p class='verticaltext' onmouseover='this.style.color=red'>Some Text To Display</p>"

but "red" has to be quoted.

When you use echo on it you put all of it in doubles and turn the doubles into singles. But what do you do with the singles? I tried using multiple single quotes and also tried just taking them out. Nothing worked. How is this done?
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: html to php - dealing with quotes problem

Post by ell0bo »

do this...

echo <<<HTML
<div id="whatever">Hello World</div>
HTML;

Or if you want to put it into a variable...

$var = <<<HTML
<div id="whatever">Hello World</div>
HTML;
avianrand
Forum Newbie
Posts: 3
Joined: Tue Jun 29, 2010 2:05 pm

Re: html to php - dealing with quotes problem

Post by avianrand »

Thanks for the info. I couldn't get that to work. Blank page. But this works:

echo "<p class='verticaltext' onmouseover='this.style.color="red"'>" . $name_char . "</p>";
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: html to php - dealing with quotes problem

Post by ell0bo »

did you put a new line after the HTML; ???

You server was prolly tossing out an error...
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: html to php - dealing with quotes problem

Post by AbraCadaver »

There are several ways to do this. You can use single quotes inside of double quotes or the other way around. If you need singles in singles or doubles in doubles etc. then you need to escape them:

Code: Select all

echo 'something \'something\' and "something else" something';
//or
echo "something 'something' and \"something else\" something";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
avianrand
Forum Newbie
Posts: 3
Joined: Tue Jun 29, 2010 2:05 pm

Re: html to php - dealing with quotes problem

Post by avianrand »

Turns out escaping is the best way for what I'm trying to do. Took some doing to figure out what to escape and what not to escape but it's working nicely. Thanks.
Post Reply