Search code examples
phpquotation-marks

php multi Quotation marks


I meet a headache problem. there are too many Quotation marks in my code make me headache.

I tried both of these method, but all the way are make links broken. I cheked it in chrome, In elements, I find the source code like what I add after print($link);.

How to solve the problem? Thanks.

$str = 'I\'m very "shock"!';
$link=<<<EOT
<a Onclick="javascript('$str')" href="#">$str</a>'
EOT;
print($link); // <a onclick="javascript('I'm very " shock"!')"="" href="#">I'm very "shock"!</a>

OR

$str = 'I\'m very "shock"!';
$link = '<a Onclick="javascript(\''.$str.'\')" href="#">'.$str.'</a>';  
print($link); //<a onclick="javascript('I'm very " shock"!')"="" href="#">I'm very "shock"!</a>

Solution

  • I would do this:

    $link = '<a Onclick="javascript(\''.addslashes($str).'\')" href="#">'.$str.'</a>';