Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

How Do I Automatically Escape Special Characters?

Community Beginner ,
Feb 28, 2010 Feb 28, 2010

Copy link to clipboard

Copied

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

TOPICS
Server side applications

Views

3.9K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Mar 02, 2010 Mar 02, 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>";

...

Votes

Translate
Community Beginner ,
Mar 02, 2010 Mar 02, 2010

Copy link to clipboard

Copied

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...?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 02, 2010 Mar 02, 2010

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 02, 2010 Mar 02, 2010

Copy link to clipboard

Copied

Thanks David,

That's exactly what I needed to know.

The solution I came down to was using heredoc ( .=<<< )  and wrapping variable with {}.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 03, 2012 Aug 03, 2012

Copy link to clipboard

Copied

"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?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 03, 2012 Aug 03, 2012

Copy link to clipboard

Copied

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;

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 06, 2012 Aug 06, 2012

Copy link to clipboard

Copied

I only posted a snippet of the code I used, but HEREDOC does not work with my code.

$body = <<<END

<html>

    <head>

      <title>Online shipping calculator.</title>

    </head>

<style>

body {

    width: 700px;

    margin: 0 auto;

}

label {

    float: left;

    clear: both;

    width: 250px;

}

table {

    margin: 0 0 15px 0;

}

</style>

<body>

<h1>Shipping Calculator</h1>

<table width="700" border="0" cellpadding="5">

  <tr>

    <td colspan="2"><h3>Basic Information</h3></td>

  </tr>

  <tr>

    <td width="500">Monthly Carrier Shipping Spend<br>(i.e. FedEx, UPS, USPS, other)</td>

    <td align="right">{$ups}</td>

  </tr>

  <tr>

    <td width="500">Total Annual Shipping Spend</td>

    <td align="right"> {$totalvolume} </td>

  </tr>

  <tr>

    <td width="500">Customer's Cost to Process a Check</td>

    <td align="right">{ $checkctp }</td>

  </tr>

  <tr>

    <td width="500"># of Checks Processed Monthly for Shipping</td>

    <td align="right">{$checkppm}</td>

  </tr>

  <tr>

    <td width="500"><h3>Savings</h3></td>

    <td align="right"></td>

  </tr>

  <tr>

    <td width="500">Annual Savings in Check Processing</td>

    <td align="right">{ $peryear }</td>

  </tr>

  </table>

  END;

The DW syntax is all messed up.

Many times, you still have to escape characters, due to apostrophes in the text or other html in the code...so whether you start with double or single quotes, you still need to escape characters. The Heredoc doesn't work in my example.

My point about your arrogance wasnt necessarily whether you were right or wrong, but your solution to the original post was "do it a different way". Obviously, I came here because I was looking for the same answer the original poster was. A way to automatically escape characters. So its obvious more than one person would like this feature.... Dreamweaver already has automatic open tag removal and source formatting. All you did was scold the OP and told him "do it a different way"... when a simple "No, Dreamweaver doesnt have this feature, but you can try it this way...."

Instead, you went the route of "longer answer learn to use PHP more efficiently"... IMO, a personal dig similar to the elitist rants by children in online forums. All you needed to do was add a "noob" at the end to really work it.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 07, 2012 Aug 07, 2012

Copy link to clipboard

Copied

LATEST

greekdish wrote:

I only posted a snippet of the code I used, but HEREDOC does not work with my code.

Can you be a little more precise about what "doesn't work"?

I have copied and pasted your code into Dreamweaver CS6, and tested it with some dummy values for the variables. With a couple of minor tweaks, the heredoc syntax does work. There must be no spaces before closing identifier (in this case, END) on the final line. When I pasted your code, it was preceded by two spaces. However, I accept that might simply be a result of the way code is formatted in the forum post.

The other point was the use of curly braces. You only need them around variables that reference array elements. For example $row['item'] needs braces, but $ups doesn't. However, if you put braces around simple variables, such as $ups, there must be no space between the braces and the variable. In a couple of cases, you have spaces. As a result, the braces are treated as plain text, and are printed out when the $body variable is echoed.

The DW syntax is all messed up.

Yes, that's a known issue. The syntax colouring doesn't support heredoc syntax, but that doesn't affect the validity of the code. Please file a bug report using the form at http://adobe.ly/DWBug. The more people complain about heredoc syntax colouring, the more likely Adobe is to address the problem.

All you did was scold the OP and told him "do it a different way"... when a simple "No, Dreamweaver doesnt have this feature, but you can try it this way...."

Instead, you went the route of "longer answer learn to use PHP more efficiently"... IMO, a personal dig similar to the elitist rants by children in online forums. All you needed to do was add a "noob" at the end to really work it.

Maybe your way of presenting the solution would have been better. I acknowledge that. But the original poster didn't take my reply as an insult or  scold. He actually thanked me and said "that's exactly what I needed to know."

You've only posted a couple of times since joining more than two years ago, so you might not be familiar with the atmosphere in the Dreamweaver forums. Most of the people who provide solutions here are generous with their time and advice. There's also a generally friendly and supportive atmosphere. But Dreamweaver can't do everything for you. The solution to many problems is to learn how to use the underlying technology, be it PHP, HTML, CSS, or JavaScript, more efficiently.

That's not an elitist view. It's a fact of life. There has been a distinct shift in Dreamweaver's focus over the past few versions. It's now aimed much more at the professional user who has a solid understanding of web technologies. From my discussions with Adobe engineers, I know that trend is set to continue.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines