Skip to content
Advertisement

PHP – preg_replace for /[[:punct:]]/ isn’t removing double quotes

I’m trying to strip punctuation in the following string "Peter !!!!!!!! MacIntyre" (including the quotes at the end of the string). I’m using this code:

$filterItem = filter_var($searchTearm, FILTER_SANITIZE_STRING);
$duplicatePunctuation = preg_replace('/[[:punct:]]/', ' ', strval($filterItem));
$output = $duplicatePunctuation;

The output I’m getting is 34 Peter MacIntyre 34. The exclamation marks are replaced as it should be. However, the double quotes are being converted into the number 34. I tried str_replace but it wouldn’t remove them.

I also tried $duplicatePunctuation = str_replace(chr(34), ' ', strval($filterItem));

Advertisement

Answer

I managed to solve it by using

$removeDoubleQuotes = trim(html_entity_decode($filterItem), '"');
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement