I am trying to pass a string from a PHP variable to HTML
<a href="javascript:deleteProduct('<?=addslashes开发者_如何学Go($row['productName'])?>');"
The problem is with the apostrophes. The string might contain both single and double apostrophes ' and " , like in the following example:
<a href="javascript:deleteProduct('Richdel, 2400\', 1\", fi fara solenoid');"
It won't trigger the Javascript function due to the incorrect use of the apostrophes, syntax error. It processes as the \" would be the end of the href attribute's value.
How can I fix this problem?
<?= htmlentities(addslashes($row['productName'])) ?>
will turn the "
s into "
, so they'll go into HTML.
A simple string replace will remove both types of quote entirely.
<?php echo str_replace("'", '', str_replace('"', '', $row['product'])); ?>
精彩评论