0

I'm using html_entity_decode to decode html codes, and then using preg_replace, but for some reason it's replacing all my single quotes for some reason. What am I doing wrong?

<?php

$userResponse = "I&#039;m tired.";
$userResponse = html_entity_decode($userResponse); //make codes like &#039; into '
$userResponse = preg_replace("~[^*a-zA-Z,.!?' ]~", "", $userResponse);
echo $userResponse;

?>

Result: Im tired.

Expected Result: I'm tired.

frosty
  • 2,559
  • 8
  • 37
  • 73
  • 1
    Have you checked `$userResponse` after `html_entity_decode()`ing it? On CLI I still get `"I'm tired."` Had to `$userResponse =html_entity_decode($userResponse, ENT_QUOTES | ENT_XML1, 'UTF-8');` to fix it – brombeer Jan 20 '18 at 17:54
  • You do realize that you're stripping the apostrophe specifically? – mario Jan 20 '18 at 17:58
  • @kerbholz Yeah, I added your code and it works. Yeah, I don't know why it didn't turn ' into ' with the regular html_entity_decode. – frosty Jan 20 '18 at 18:01
  • 1
    @mario Well, not really. This is a user input variable, so I'm stripping everything that's not excluded in the preg_replace. – frosty Jan 20 '18 at 18:02

0 Answers0