str_replace and htmlspecialchars not working

79 Views Asked by At

OK, I know that what I see is not necessarily what the code says. I got that part figured out. But I have a long list of characters that need to be stored in a database as their html entities rather than as the punctuation marks. I have updated my code to this:

$searchval = array("á","–","—","/"," ","-","...","…","\&","\'","í","\[","\~","\"","\]");
  $replaceval = array("\&egrave\;","\&ndash\;","\&mdash\;","\&#47","\&#nbsp\;","\&#hyphen\;","\&hellip\;","\&hellip\;","\&amp\;","\&apos\;","\&igrave\;","\&lbrack\;","\&ntilde\;","\&quot\;","\&rbrack\;");
  str_replace($searchval,$replaceval,$fixed);

But the entitiy names are not going into the database.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'most # rig&ht...'

How do I get the entities to go into the database rather than the punctuation marks?

<!DOCTYPE html>
<html>
<body style="margin-left:50px">

<?php

     $fixed = "this is al'most right";
     echo $fixed;
     echo "<br>";
     echo "htmlspecialchars: ";
     echo htmlspecialchars($fixed, ENT_QUOTES);
     echo "<br>";
     echo "str_replace: ";
     echo str_replace("'", "&apos;", $fixed);
     echo "<br>";


?>

</body>
</html>
0

There are 0 best solutions below