PHP Replace special characters before MySQL insertion

If you are working with MySQL database (UTF-8) and handle input that is being copied from other sources- you may run into issues with special characters such as ellipses, long dashes, and special quotes.

Inserting these characters without addressing them can lead to nasty results later on, like this: 


Here is a useful function that replaces the most common special characters with their UTF equivalents

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// replace special characters
function replace_schars($str)
{
     $badwordchars=array(
         "\xe2\x80\x98", // left single quote
         "\xe2\x80\x99", // right single quote
         "\xe2\x80\x9c", // left double quote
         "\xe2\x80\x9d", // right double quote
         "\xe2\x80\x93", // long dash
         "\xe2\x80\x94", // long dash
         "\xe2\x80\xa6" // elipses
     );
     $fixedwordchars=array(
         "'",
         "'",
         '"',
         '"',
         '-',
         '-',
         '...'
     );
     return (str_replace($badwordchars,$fixedwordchars, $str));
}
// replace special characters
function replace_schars($str)
{
     $badwordchars=array(
         "\xe2\x80\x98", // left single quote
         "\xe2\x80\x99", // right single quote
         "\xe2\x80\x9c", // left double quote
         "\xe2\x80\x9d", // right double quote
		 "\xe2\x80\x93", // long dash
		 "\xe2\x80\x94", // long dash
         "\xe2\x80\xa6" // elipses
     );
     $fixedwordchars=array(
         "'",
         "'",
         '"',
         '"',
		 '-',
         '-',
         '...'
     );
     return (str_replace($badwordchars,$fixedwordchars, $str));
}
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">