簡體   English   中英

php變量中的單引號和雙引號

[英]single and double quotes in php variable

如何將此代碼與所有單引號和雙引號結合使用。 我嘗試了幾種組合,但我無法使其發揮作用。 這是我的最后一次嘗試所以請幫助。

使用長字符串時,什么是好方法?

$html .='<a href="" onClick="$.ajax({type: "POST",url: "delete_pic.php",data:{id:"'.$row['id'].'",var:"V"},cache: false});" style="background:url("images/icons/delete.png" 50% -19px no-repeat;width:16px;height:16px;float:left;margin-left:10px;margin-top: 6px;"></a>';

我會將你的樣式移動到外部樣式表以縮短它,然后只是在字符串中轉義“\\”“for”之類的引號。

$html .="<a href=\"\" onClick=\"$.ajax({type: \"POST\",url: \"delete_pic.php\",data:{id:\".$row["\id\"].",var:\"V\"},cache: false});\" class=\"mystyle\"></a>";

這沒有測試,因為我沒有你的代碼:)

最好的解決方案是使用HEREDOC ,它完全不需要在PHP級別轉義任何引用:

$html .= <<<EOL
<a href="onclick('\$.ajax({ etc.....

EOL;

請注意,您仍然受到嵌入在heredoc中的任何語言的引用需求的約束。 但至少你不必擔心由於不平衡/未轉義的引號導致PHP語法錯誤。

我遵循以下規則:php字符串封裝在單引號中,因此html的屬性是雙引號。
屬性中的任何引號都必須是轉義單引號\\'

所以:

$html .='<a href="" onClick="$.ajax({type: \'POST\',url: \'delete_pic.php\',data:{id:\''.$row['id'].'\',var:\'V\'},cache: false});" style="background:url(\'images/icons/delete.png\' 50% -19px no-repeat;width:16px;height:16px;float:left;margin-left:10px;margin-top: 6px;"></a>';

你應該只是逃避其他雙引號內的雙引號(如果這是有道理的)。 :)

$html .='<a href="" onClick="$.ajax({type: \"POST\",url: \"delete_pic.php\",data:{id:\"'.$row['id'].'\",var:\"V\"},cache: false});" style="background:url(\"images/icons/delete.png\" 50% -19px no-repeat;width:16px;height:16px;float:left;margin-left:10px;margin-top: 6px;"></a>';

那(或類似的東西)應該有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM