PHP Issues With Decoding Strange Smart Quotes And Non-Standard ASCII Characters

When using strip_tags and html_entity_decode with PHPit often breaks and produces annoying diamonds with question marks.

It is probably because of characters like these:

… (looks like 3 dots but it is a single weird character).

’ (looks like a normal apostraphe but it is not)

” (looks like a normal double quote but it is not).

An easy way to sort this out is to copy the above and search in an ASCII table to extend the functionality below.

Basically the first array has a list of "all the bad stuff" and the second array is what to replace it with.



function CleanupSmartQuotes($text)
{
$badwordchars=array(
chr(145), // weird apostraphe
chr(146), // weird apostraphe
chr(147), // weird quote
chr(148), // weird quote
chr(151), // weird dash
chr(133), // three dots
' ',
);
$fixedwordchars=array(
"'",
"'",
'"',
'"',
'—',
'...',
'',
);
return str_replace($badwordchars,$fixedwordchars,$text);
}

?>


Tags:

php, decoding, quotes, ascii, characterswhen, strip_tags, html_entity_decode, produces, characters, hellip, dots, rsquo, apostraphe, rdquo, extend, functionality, array, quot, cleanupsmartquotes, text, badwordchars, chr, dash, fixedwordchars, mdash, str_replace,

Latest Articles

  • Linux Ubuntu Cannot Print Large Images
  • Cannot Print PDF Solution and Howto Resize
  • Linux Console Login Screen TTY Change Message
  • Apache Cannot Start Listening Already on 0.0.0.0
  • MySQL Bash Query to pipe input directly without using heredoc trick
  • CentOS 6 and 7 / RHEL Persistent DHCP Solution
  • Debian Ubuntu Mint rc-local service startup error solution rc-local.service: Failed at step EXEC spawning /etc/rc.local: Exec format error
  • MySQL Cheatsheet Guide and Tutorial
  • bash script kill whois or other command that is running for too long
  • Linux tftp listens on all interfaces and IPs by DEFAULT Security Risk Hole Solution
  • python import docx error
  • Cisco Unified Communications Manager Express Cheatsheet CUCME CME
  • Linux Ubuntu Debian Missing privilege separation directory: /var/run/sshd
  • bash how to count the number of columns or words in a line
  • bash if statement how to test program output without assigning to variable
  • RTNETLINK answers: Network is unreachable
  • Centos 7 how to save iptables rules like Centos 6
  • nfs tuning maximum amount of connections
  • qemu-kvm error "Could not initialize SDL(No available video device) - exiting"
  • Centos 7 tftpd will not work with selinux enabled