在PHP中使用单引号(转义)

时间:2022-02-18 22:29:09

I am writing HTML code inside PHP tags. Already for anchor tag styling is written and if I change some parts will affect. So I am trying to write my code inside the span onclick event. Here is my code

我在PHP标签内编写HTML代码。已经为锚标签样式写了,如果我改变一些部分会影响。所以我想在span onclick事件中编写我的代码。这是我的代码

 <div>
    <span style='cursor:pointer;' onclick='window.location.href='www.google.com'>
       ".$array1[$i] ['name']."
    </span>
 </div>

If that array[name] is clicked, it should go to google.com. The problem is the single quotes I used for mentioning my URL. How do I escape strings in this event?

如果单击该数组[名称],则应转到google.com。问题是我用来提及我的URL的单引号。如何在此事件中转义字符串?

4 个解决方案

#1


2  

    $strLocation = 'http://www.google.com';
    $strLink = "<span onclick='window.location.href=&#39;".$strLocation."&#39;'>HI there</span>";

(or)

$strLink = '<span onclick="window.location.href=\''.$strLocation.'\'">HI there</span>';

    print $strLink;

Using &#39; is an HTML special character which remains undetected during string operations and will appear only in the browser.

使用'是一个HTML特殊字符,在字符串操作期间仍未检测到,并且仅出现在浏览器中。

Using \ simply escapes the string.

使用\只是转义字符串。

#2


3  

I think you already got the code. However a bit more explanation:

我想你已经有了代码。但是更多的解释:

(i) There are two types of quotes i.e. single quotes (' ... ') and double quotes (" ... "). Now when you are using one style of quote as outer quote, you can use the other style of quote inside that and you don't need to escape any quote.

(i)有两种类型的引号,即单引号('...')和双引号(“......”)。现在,当您使用一种报价方式作为外部报价时,您可以在其中使用其他报价方式,而您无需转义任何报价。

e.g. echo 'He said "What a lovely day"'; output: He said "What a lovely day"

例如回声'他说'多么美好的一天'';输出:他说“多么美好的一天”

echo "She said 'that is true'";

output: She said 'that is true'

输出:她说'那是真的'

(ii) However in the second case if you wanted to output -> She said 'that's true' You need to escape the quote. In php default escape character is \

(ii)然而在第二种情况下,如果你想输出 - >她说'那是真的'你需要逃避报价。在php默认转义字符是\

As you have already noted the quotes have special meaning to PHP. To remove the special meaning we 'escape' the character.

正如您已经注意到,引号对PHP具有特殊意义。为了消除特殊意义,我们“逃脱”了角色。

So you need to write: echo 'She said "that\'s true"';

所以你需要写:echo'她说'那是真的'';

or

echo "She said \"that's true\"";

This the basic concept behind escaping. But please note that the two types of quotes have different meaning in some cases. A double quote executes in the content inside them whereas a single quote assumes that the content inside them is not to be evaluated.

这是逃避背后的基本概念。但请注意,在某些情况下,这两种类型的引号具有不同的含义。双引号在其内部的内容中执行,而单引号则假定其中的内容不被评估。

i.e.

$i = 1;

echo "$i";

// output: 1

echo '$i';

// output: $i

Keep this in mind when writing codes.

编写代码时请记住这一点。

#3


1  

Why are you using single quotes in the first place?

你为什么一开始使用单引号?

<div>
    <span style="cursor:pointer;" onclick="window.location.href='www.google.com'">
        <?php echo $array1[$i]['name'] ?>
    </span>
</div>

That's all you need; there's no need to complicate things more than that :)

这就是你所需要的;没有必要让事情复杂化了:)


Per you comment, you're using this inside an echo. I think that's a bit silly, but in that case, use heredoc syntax

根据你的评论,你在回声中使用它。我认为这有点傻,但在这种情况下,请使用heredoc语法

echo <<<EOD
    <div>
        <span style="cursor:pointer;" onclick="window.location.href='www.google.com'">
            <?php echo $array1[$i]['name'] ?>
        </span>
    </div>
EOD;

#4


0  

Use json_encode and it will reliably manage the escaping for you.

使用json_encode,它将可靠地为您管理转义。

<? $exampleA = "Escape this 's"; ?>
<? $exampleB = 'Escape this "s'; ?>
<script>
var a = <?= json_encode($exampleA) ?> // result: var a = "Escape this 's"
var b = <?= json_encode($exampleB) ?> // result: var b = "Escape this \"s"
</script>

Found here: https://*.com/a/6269254/922522

在此处找到:https://*.com/a/6269254/922522

#1


2  

    $strLocation = 'http://www.google.com';
    $strLink = "<span onclick='window.location.href=&#39;".$strLocation."&#39;'>HI there</span>";

(or)

$strLink = '<span onclick="window.location.href=\''.$strLocation.'\'">HI there</span>';

    print $strLink;

Using &#39; is an HTML special character which remains undetected during string operations and will appear only in the browser.

使用'是一个HTML特殊字符,在字符串操作期间仍未检测到,并且仅出现在浏览器中。

Using \ simply escapes the string.

使用\只是转义字符串。

#2


3  

I think you already got the code. However a bit more explanation:

我想你已经有了代码。但是更多的解释:

(i) There are two types of quotes i.e. single quotes (' ... ') and double quotes (" ... "). Now when you are using one style of quote as outer quote, you can use the other style of quote inside that and you don't need to escape any quote.

(i)有两种类型的引号,即单引号('...')和双引号(“......”)。现在,当您使用一种报价方式作为外部报价时,您可以在其中使用其他报价方式,而您无需转义任何报价。

e.g. echo 'He said "What a lovely day"'; output: He said "What a lovely day"

例如回声'他说'多么美好的一天'';输出:他说“多么美好的一天”

echo "She said 'that is true'";

output: She said 'that is true'

输出:她说'那是真的'

(ii) However in the second case if you wanted to output -> She said 'that's true' You need to escape the quote. In php default escape character is \

(ii)然而在第二种情况下,如果你想输出 - >她说'那是真的'你需要逃避报价。在php默认转义字符是\

As you have already noted the quotes have special meaning to PHP. To remove the special meaning we 'escape' the character.

正如您已经注意到,引号对PHP具有特殊意义。为了消除特殊意义,我们“逃脱”了角色。

So you need to write: echo 'She said "that\'s true"';

所以你需要写:echo'她说'那是真的'';

or

echo "She said \"that's true\"";

This the basic concept behind escaping. But please note that the two types of quotes have different meaning in some cases. A double quote executes in the content inside them whereas a single quote assumes that the content inside them is not to be evaluated.

这是逃避背后的基本概念。但请注意,在某些情况下,这两种类型的引号具有不同的含义。双引号在其内部的内容中执行,而单引号则假定其中的内容不被评估。

i.e.

$i = 1;

echo "$i";

// output: 1

echo '$i';

// output: $i

Keep this in mind when writing codes.

编写代码时请记住这一点。

#3


1  

Why are you using single quotes in the first place?

你为什么一开始使用单引号?

<div>
    <span style="cursor:pointer;" onclick="window.location.href='www.google.com'">
        <?php echo $array1[$i]['name'] ?>
    </span>
</div>

That's all you need; there's no need to complicate things more than that :)

这就是你所需要的;没有必要让事情复杂化了:)


Per you comment, you're using this inside an echo. I think that's a bit silly, but in that case, use heredoc syntax

根据你的评论,你在回声中使用它。我认为这有点傻,但在这种情况下,请使用heredoc语法

echo <<<EOD
    <div>
        <span style="cursor:pointer;" onclick="window.location.href='www.google.com'">
            <?php echo $array1[$i]['name'] ?>
        </span>
    </div>
EOD;

#4


0  

Use json_encode and it will reliably manage the escaping for you.

使用json_encode,它将可靠地为您管理转义。

<? $exampleA = "Escape this 's"; ?>
<? $exampleB = 'Escape this "s'; ?>
<script>
var a = <?= json_encode($exampleA) ?> // result: var a = "Escape this 's"
var b = <?= json_encode($exampleB) ?> // result: var b = "Escape this \"s"
</script>

Found here: https://*.com/a/6269254/922522

在此处找到:https://*.com/a/6269254/922522