Question

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>
Was it helpful?

Solution

I would do this:

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

OTHER TIPS

You need to double-escape your quotes :

$str = 'I\\\'m very "shock"!';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top