admin管理员组

文章数量:1406032

How can I bine this code with all single and double quotes as it should be. I have tried several binations and I can't make it work. This one is my last try so please help.

What would be a good approach when working with long strings?

$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>';

How can I bine this code with all single and double quotes as it should be. I have tried several binations and I can't make it work. This one is my last try so please help.

What would be a good approach when working with long strings?

$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>';
Share Improve this question asked Apr 24, 2013 at 16:39 user123_456user123_456 5,82526 gold badges87 silver badges142 bronze badges 2
  • I think the problem is in the style attribute part. The opening quotation for the url of the background closes the opening quotation of the style attribute. Maybe you should follow @Jeffrey's answer below, and separate your stylesheet rather than make it inline :) – Arnelle Balane Commented Apr 24, 2013 at 16:44
  • 1 2 notes here: you are missing the closing ) on the url(, and as strange as it seems, you dont need any quotes around the url – Uberfuzzy Commented Apr 24, 2013 at 16:46
Add a ment  | 

4 Answers 4

Reset to default 3

I would move your styles to an external stylesheet to make it shorter, and then just escape the quotes like "\"" for " in the string.

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

This was not tested because I don't have your code :)

Best solution is to use HEREDOC, which pletely eliminates the need for ANY quote escaping at the PHP level:

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

EOL;

Note that you'll still be bound by the quoting needs of whatever language(s) you're embedding in the heredoc. But at least you won't have to worry about causing a PHP syntax error because of unbalanced/unescaped quotes.

I follow the rule of: php strings are encapsulated in single quote, so attributes of html are in double quotes.
Any quote in the attribute must be an escaped single quote \'

so:

$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>';

You should probably just escape the double-quotes inside the other double-quotes (if that makes sense). :)

$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>';

That (or something similar) should work.

本文标签: javascriptsingle and double quotes in php variableStack Overflow