Skip to main content
Participant
February 28, 2010
Answered

How Do I Automatically Escape Special Characters?

  • February 28, 2010
  • 1 reply
  • 4298 views

Hi, I have tried searching for an answer to this and thought I would post this question before giving up.  Is there any way to automatically insert the escape characters into an HTML string for PHP processing?

In other words if I have the following line:

<div id="myid" clas="myclass"></div>

Is there any way to automate turning it into the following:

<div id=\"myid\" clas=\"myclass\"></div>

This is just so tedious and yet basic and I'm assuming/hoping Dreamweaver has some way of making this easy for us...

Thanks

This topic has been closed for replies.
Correct answer David_Powers

You probably didn't get any responses because you posted in the wrong forum. The Dreamweaver Application Development forum deals with PHP and other server-side technologies. I have moved your thread to the right place.

The short answer to your question is "no". The longer answer is to learn how to use PHP more efficiently.

PHP is designed to be embedded in HTML. There is very little to be gained from using PHP to generate static text like this:

<?php

echo "<div id=\"myid\" class=\"myclass\"></div>";

?>

Use plain HTML for the static part and PHP for the dynamic part. For example:

<div id="myid" class="myclass"><?php echo $someText; ?></div>

However, if there is a reason for including your HTML in a PHP string, make use of single and double quotes like this:

<?php

echo '<div id="myid" class="myclass"></div>';

?>

You can use double quotes inside a single-quoted string (and vice versa) to your heart's content.

1 reply

zeeroeAuthor
Participant
March 2, 2010

Hey, I haven't gotten any responses and i was wondering if either my question didn't make sense or if Dreamweaver just doesn't have this feature...?

David_Powers
David_PowersCorrect answer
Inspiring
March 2, 2010

You probably didn't get any responses because you posted in the wrong forum. The Dreamweaver Application Development forum deals with PHP and other server-side technologies. I have moved your thread to the right place.

The short answer to your question is "no". The longer answer is to learn how to use PHP more efficiently.

PHP is designed to be embedded in HTML. There is very little to be gained from using PHP to generate static text like this:

<?php

echo "<div id=\"myid\" class=\"myclass\"></div>";

?>

Use plain HTML for the static part and PHP for the dynamic part. For example:

<div id="myid" class="myclass"><?php echo $someText; ?></div>

However, if there is a reason for including your HTML in a PHP string, make use of single and double quotes like this:

<?php

echo '<div id="myid" class="myclass"></div>';

?>

You can use double quotes inside a single-quoted string (and vice versa) to your heart's content.

David_Powers
Inspiring
August 3, 2012

"The short answer to your question is "no". The longer answer is to learn how to use PHP more efficiently."

Thats a pretty arrogant answer, and also the wrong answer. Your examples left out one crucial method where you will need to escape quotes.....sending out html email with php.

Here is just a short snippet of what I am working on....

$msg .= "<table width=\"700\" border=\"0\" cellpadding=\"5\" style=\"color: #FFF; background: #009933\">

  <tr>

    <td><h2>Total Annual Savings:</h2></td>

    <td><h2>$totalsavings</h2></td>

  </tr>

</table>";

mail($to, $subject, $msg, $headers);

So how do you propose we handle this without escaping characters? It's either escaping characters, or having to go concatenating the php variables....what's the difference?


greekdish wrote:

"The short answer to your question is "no". The longer answer is to learn how to use PHP more efficiently."

Thats a pretty arrogant answer, and also the wrong answer.

Sorry if you found my answer arrogant, but it's also pretty arrogant to accuse me of having given the wrong answer.

If you read my full post carefully, you'll see that the last example shows how to use a combination of single and double quotes. The snippet you're working on can very simply be adapted like this:

$msg .= "<table width='700' border='0' cellpadding='5' style='color: #FFF; background: #009933'>

  <tr>

    <td><h2>Total Annual Savings:</h2></td>

    <td><h2>$totalsavings</h2></td>

  </tr>

</table>";

mail($to, $subject, $msg, $headers);

Alternatively, you could use heredoc syntax as the original poster did:

$msg .= <<<END

<table width="700" border="0" cellpadding="5" style="color: #FFF; background: #009933">

  <tr>

     <td><h2>Total Annual Savings:</h2></td>

     <td><h2>$totalsavings</h2></td>

   </tr>

</table>

END;