Quoting quotation marks

限于喜欢 提交于 2019-12-12 04:54:09

问题


Anyone got a way of thinking about this?

I'm going a bit bats working on this:

$toReturn .= "
    function addProd(pExists) 
    {
    document.getElementById('products').innerHTML = \"<tr><td id='prod_n'><input type='text' size='10' maxlength='10' name='proj_n' id='prod_n' onchange=".chr(92).chr(34)."saveData(setSaveParams('ajaxSaveDataController.php', 'PROD', 'n'), this.value)".chr(92).chr(34)." value='";

$toReturn .="'></td>

                                    <td id='prod_spec'>    <textarea cols='60' name='prod_spec' id='prod_spec' rows='20' onchange=".chr(92).chr(34)."saveData(setSaveParams('ajaxSaveDataController.php', 'PROD', 'prod_spec'), this.value)".chr(92).chr(34)." value='";                                    

$toReturn .="'></td></tr>\" + document.getElementById('prodsTab').innerHTML;

if (pExists == 0)
{
    document.getElementById('prodsTab').innerHTML = \"<tr><th id='proj_spec_h'>Name</td><th id='proj_spec_h'>Spec</td></tr>\" + document.getElementById('prodsTab').innerHTML;";

I've transcribed that so don't worry overmuch about an off topic typo.

What's going on here is I'm using PHP to write Javascript that calls an Ajax function, and what's blowing me away is the "s and the 's

So I'm writing a return string, in PHP, so all that's in "

Then I want to write some innerHTML, so that I can put inside \"

Then the HTML arguments can be in '

All's fine so far.

So then my onchange call is supposed to end up as onchange="function('a', 'b')", well, where are my "s going to come from? If I use " that'll end the PHP string. If I use \" that'll end the innerHTML string. So I ended up getting PHP to interpret this chr(92).chr(34)

So that's very messy and it's starting to hurt my head.

And now it's failing on that blank line after the second $toReturn, which I wanted there for readability.

I must be doing something wrong in terms of style or, something.

How do you handle nested "s?


回答1:


Use a double escaped double quotes:

\\\"

In this way when this will get printed, it will become a simple escaped double quote (\") and it won't be in the innerHTML.




回答2:


Consider using the HEREDOC syntax. http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

<?php

$my_string = <<<EOD
    <script type="text/javascript">
        var x = "this string has a ' known as a single quote!";
    </script>
EOD;


?>

The advantage with this syntax is being able to freely write your html or javascript without escaping quotes.

Update

Here is a little more info on HEREDOC.

First and foremost the most common error related to heredoc syntax is caused by whitespace. HEREDOC syntax requires an opening and closing identifier. In the example above I have used EOD as the identifier. It is important that the opening identifier has no whitespace after it and the closing identifier is on a new line, has no whitespace before it and no whitespace between the identifier and the semi-colon.

You do not have to use EOD as your identifier. You can use something more descriptive. I like to use an identifier that describes the block of code.

<?php

$html = <<<HTML
    <html>
        <head>
            <title>Example html</title>
        </head>
        <body>
            <p class="paragraph">This is my html!</p>
        </body>
    </html>
HTML;

$javascript = <<<JAVASCRIPT
    <script type="text/javascript">
        var helloText = "Hello, my name is Jrod";
        alert(helloText);
    </script>
JAVASCRIPT;

?>

To use variables inside your heredoc syntax you will need to use curly braces around your variable in the code.

$widget = <<<WIDGET
    <p>Good Morning {$name}</p>
WIDGET;



回答3:


Have you considered tried quoting the quotes Such as \\\"?

After one step, this will usually become \", and after the second step ".

Anyway, generating (javascript) code inside (html) code from (php) code is bound to become messy. Consider using a templating system, or separating concerns.




回答4:


Another trick is defining the strings in javascript before putting them into the onchange, then using single quotes there

<script type='text/javascript'>
<!--
var a = 'a';
var b = 'b';
-->
</script>
...
<... onchange='function(a, b)'>


来源:https://stackoverflow.com/questions/8994777/quoting-quotation-marks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!