【问题标题】:issue with single double quotes in phpphp中单双引号的问题
【发布时间】:2013-09-24 12:15:26
【问题描述】:

我在函数及其参数中遇到单引号和双引号问题。这是我的代码

$prev_page_arrow= "<span class='pagn_prev' onclick=\"send($where,'Page_{$this->name}=$prev&amp;{$this->rppGet}{$this->query_string}#$this->anchor');\" >Prev</span>";

问题在于send 函数中的$where 参数。 $where 变量的值类似于

$where = purpose="buy" and city="cityname"

$where = purpose='buy' and city='cityname'

我可以在singledouble 引号中创建$where 属性值。问题是当值在double quotes 中时,它会结束函数参数。例如

$prev_page_arrow= "<span class='pagn_prev' onclick=\"send(purpose="buy",'Page_{$this->name}=$prev&amp;{$this->rppGet}{$this->query_string}#$this->anchor');\" >Prev</span>";

这里引用的double 表示带参数的函数结束。同样,如果我使用single 引用,则表示参数结束。

如何解决?

【问题讨论】:

  • 请避免以这种方式混合 HTML 和 PHP。使用“.addslashes($where)”。也许?
  • 我的方法有什么解决方案吗? ;)
  • 我编辑了第一条评论。基本上,由其他字符串变量组成的字符串变量也由另一个变量组成,这不是一个好主意。太乱了

标签: php html


【解决方案1】:

我建议将您的“where”值放在 data-* 属性中,而不是尝试将其嵌入内联事件处理程序中。就此而言,无论如何您都不应该使用内联处理程序。

重点是,试试这个:

$prev_page_arrow="<span class='pagn_prev' data-where=\"".htmlspecialchars($where)."\"
     onclick=\"send(this.getAttribute('data-where'),'Page_........');\">Prev</span>";

【讨论】:

    【解决方案2】:

    也许这会起作用?

    $prev_page_arrow= "<span class='pagn_prev' onclick='send(purpose=\"buy\", \"Page_{$this->name}=$prev&amp;{$this->rppGet}{$this->query_string}#$this->anchor\");' >Prev</span>";
    

    【讨论】:

    • 嗯,听起来不错。让我试试看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多