引号 [英] Quoting quotation marks

查看:89
本文介绍了引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人对此有想法吗?

我要为此付出一点努力:

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.

这里发生的是我正在使用PHP编写调用Ajax函数的Javascript,而让我大吃一惊的是" s和' s

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

所以我正在用PHP写一个返回字符串,所以所有内容都在"

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

然后我要编写一些innerHTML,以便可以放入\"

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

然后HTML参数可以在'

Then the HTML arguments can be in '

到目前为止一切都很好.

All's fine so far.

因此,我的onchange呼叫应该以onchange="function('a', 'b')"结尾,那么,我的"将从哪里来?如果使用",它将结束PHP字符串.如果我使用\",则会结束innerHTML字符串.所以我最终得到PHP来解释chr(92).chr(34)

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.

现在,它在第二个$toReturn之后的空白行上失败了,我希望在那里保持可读性.

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.

您如何处理嵌套的"?

推荐答案

考虑使用HEREDOC语法. http://www.php .net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

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;


?>

此语法的优点是能够自由编写您的html或javascript,而无需转义引号.

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

更新

有关HEREDOC的更多信息.

Here is a little more info on HEREDOC.

首先,与Heredoc语法相关的最常见错误是由空格引起的. HEREDOC语法需要打开和关闭标识符.在上面的示例中,我已使用EOD作为标识符.重要的是,开头标识符后面没有空格,并且结尾标识符在新行上,前面没有空格,并且标识符和分号之间没有空格.

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.

您不必使用EOD作为标识符.您可以使用更具描述性的内容. 我喜欢使用描述代码块的标识符.

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;

?>

要在Heredoc语法中使用变量,您将需要在代码中的变量周围使用花括号.

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;

这篇关于引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆