Question

Sorry if that question has been already answered but what i found on the internet didn't help me that much.I got a problem with php and simple quotes

Here is my code :

echo "<span onclick='search(\"".$value."\");'>".some stuff."</span>";

My problem is that $value is sometimes with simple quotes (they are music titles). So i got some problems with 't' stands for trouble, can't stand losing you ...

I tryed with addslashes($value) but it does not work. Any idea ? Thanks :)

Was it helpful?

Solution

try with htmlspecialchars($value,ENT_QUOTES);

it's better also to get rid of all these slashes:

?>
<span onclick='search("<?=htmlspecialchars($value,ENT_QUOTES);?>");'>
    some stuff
</span>
<?php
// back to php

OTHER TIPS

Maybe you can HTML encode ( http://php.net/manual/en/function.htmlentities.php ), it will make HTML entities out of the special characters in your titles. Be sure to decode on the recieving end though.

str_replace("'", "&#39;", $value); 

should change quotes to their XML entity...

You may use htmlentities function:

echo "<span onclick='search(\"".htmlentities($value, ENT_QUOTES)."\");'>".some stuff."</span>";

your proper syntax should be something like this. Use htmlentites($value,ENT_QUOTES) to handle the special characters

$sanitizedValue  = htmlentites($value,ENT_QUOTES);

echo "<span onclick='search(\"{$sanitizedValue}\");'>some stuff</span>";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top