当字符串中有row ['value']时,在PHP中转义字符串的正确方法是什么?

时间:2022-10-22 11:24:14

The code below is returned to an html page in an object using AJAX.

下面的代码使用AJAX返回到对象的html页面。

It doesn't seem to do what It does when I am not echoing it as PHP, which is to sort the DIVS as they are created based on the attribute 'data-sort'

当我没有将它作为PHP回应时,它似乎没有做它做的事情,这是为了根据属性'data-sort'创建它们来对DIVS进行排序

I know that the function is correct, am I perhaps escaping it incorrectly?

我知道函数是正确的,我可能错误地逃避了吗?

echo "

        newdiv = $(\'<div id=\'eventdiv\' class=\'shadow curved_sml gradient\' data-sort=\'".$row_display['TimeStamp']."\'>".$row_new['Title']." TIME: ".$row_display['TimeStamp']."</div>\');

        div_id_after = Math.floor(parseFloat(newdiv.attr('data-sort')));

        $('div[data-sort='+div_id_after+']').after(newdiv);

        ";

Regards, Taylor

问候,泰勒

2 个解决方案

#1


1  

Yes, you are escaping it incorrectly. What you have is:

是的,你错误地逃避了它。你有的是:

  • first level of quotes: double ones, in PHP,
  • 第一级报价:双重,在PHP中,
  • second level of quotes: single ones, in JavaScript,
  • 第二级报价:单一,在JavaScript中,

but you have incorrect quotes in the third level - HTML passed as string in JS, which in turn is passed as string in PHP. Also you should not escape ''s, because you enclosed them in "'s.

但是在第三级你有不正确的引号 - HTML在JS中作为字符串传递,而后者又在PHP中作为字符串传递。你也不应该逃避,因为你把它们封在了's'中。

Try the following:

请尝试以下方法:

echo "
    newdiv = $('<div id=\"eventdiv\" class=\"shadow curved_sml gradient\" data-sort=\"" . $row_display['TimeStamp'] . "\">" . $row_new['Title'] . " TIME: " . $row_display['TimeStamp'] . "</div>');

    div_id_after = Math.floor(parseFloat(newdiv.attr('data-sort')));

    $('div[data-sort=\"'+div_id_after+'\"]').after(newdiv);
";

I think this should solve your problem. But I did not test it.

我认为这应该可以解决你的问题。但我没有测试它。

#2


0  

Yes, you are - in a double-quoted string, you don't escape single quotes. Remove all of those \'s and replace them with just '.

是的,你是 - 在双引号字符串中,你不会逃避单引号。删除所有这些并用'替换它们。

#1


1  

Yes, you are escaping it incorrectly. What you have is:

是的,你错误地逃避了它。你有的是:

  • first level of quotes: double ones, in PHP,
  • 第一级报价:双重,在PHP中,
  • second level of quotes: single ones, in JavaScript,
  • 第二级报价:单一,在JavaScript中,

but you have incorrect quotes in the third level - HTML passed as string in JS, which in turn is passed as string in PHP. Also you should not escape ''s, because you enclosed them in "'s.

但是在第三级你有不正确的引号 - HTML在JS中作为字符串传递,而后者又在PHP中作为字符串传递。你也不应该逃避,因为你把它们封在了's'中。

Try the following:

请尝试以下方法:

echo "
    newdiv = $('<div id=\"eventdiv\" class=\"shadow curved_sml gradient\" data-sort=\"" . $row_display['TimeStamp'] . "\">" . $row_new['Title'] . " TIME: " . $row_display['TimeStamp'] . "</div>');

    div_id_after = Math.floor(parseFloat(newdiv.attr('data-sort')));

    $('div[data-sort=\"'+div_id_after+'\"]').after(newdiv);
";

I think this should solve your problem. But I did not test it.

我认为这应该可以解决你的问题。但我没有测试它。

#2


0  

Yes, you are - in a double-quoted string, you don't escape single quotes. Remove all of those \'s and replace them with just '.

是的,你是 - 在双引号字符串中,你不会逃避单引号。删除所有这些并用'替换它们。