开发者

Passing PHP string to HTML href" attribute apostroph problem

开发者 https://www.devze.com 2023-02-15 07:33 出处:网络
I am trying to pass a string from a PHP variable to HTML <a href=\"javascript:deleteProduct(\'<?=addslashes开发者_如何学Go($row[\'productName\'])?>\');\"

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 &quot;, 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'])); ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消