Adventures Of Carlos
Home

Replacing Bad Microsoft Characters in MySQL

If you've ever pasted a word document into a browser's text area and then insert it into a MySQL table, you've probably encountered some funky characters replace the quotes, dashes and other special characters.

This is due to Microsoft using extended character sets over ASCII 128.

In the past I'd have to manually search and replace the bad text. Today I decided to find a better solution. Searching on Google, I came across the following blog post
Replacing smart quotes, em-dashes, and ellipses with MySQL or PHP

This was exactly what I needed.

I went further to create a MySQL function. This way I could use it to convert the text before inserting it into the database.

Here's the function if you want to use it....

DELIMITER $$

DROP FUNCTION IF EXISTS `f_ReplaceMicrosoftCharacters`$$

CREATE FUNCTION `f_ReplaceMicrosoftCharacters`(mystring TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN

DECLARE myoutstring TEXT DEFAULT ';

SET myoutstring = ';

SET myoutstring = REPLACE(mystring, 0xE28098, "'");
SET myoutstring = REPLACE(myoutstring, 0xE28099, "'");
SET myoutstring = REPLACE(myoutstring, 0xE2809C, '"');
SET myoutstring = REPLACE(myoutstring, 0xE2809D, '"');
SET myoutstring = REPLACE(myoutstring, 0xE28093, '-');
SET myoutstring = REPLACE(myoutstring, 0xE28094, '--');
SET myoutstring = REPLACE(myoutstring, 0xE280A6, '...');

/*
* Next, replace their Windows-1252 equivalents.
*/

SET myoutstring = REPLACE(myoutstring, char(145), "'");
SET myoutstring = REPLACE(myoutstring, char(146), "'");
SET myoutstring = REPLACE(myoutstring, char(147), '"');
SET myoutstring = REPLACE(myoutstring, char(148), '"');
SET myoutstring = REPLACE(myoutstring, char(150), '-');
SET myoutstring = REPLACE(myoutstring, char(151), '--');
SET myoutstring = REPLACE(myoutstring, char(133), '...');

RETURN myoutstring;

END$$

DELIMITER ;

Apr 20, 2010

Comments
louis vuitton said....

Hello, I like your blog. This is a good site and I wanted to post a little note to let you know, good job! Thanks Katy Louis Vuitton vuitton

Jun 28, 2010


Categories

About Me (4)
About This Blog (4)
Apache (2)
Charities (2)
Coldfusion (7)
Dreams (3)
House Stuff (2)
Landscaping And Gardening (3)
Linux (3)
Movies/TV (2)
MySQL (6)
Past Memories (1)
Perl (1)
Perspectives and Understandings (3)
Pets and Animals (2)
Politics (1)
Random Ramblings (9)
Recipes (5)
Software Development - General (3)
Spiritual Awakenings (2)
Sports and Fitness (2)
Travel (47)
Volunteer Work (5)

Past Blogs
Replacing Bad Microsoft C (1)
How to fix a 404 File Not
Cycling to Park Lake Clea (1)
Blog Status
Software Development Book (3)
Agile Software Developmen
Last Night I was Christop
The Vegans Have Better Se
Scoping Makes Me And You
Agile Software Developmen


Categories

About Me (4)
About This Blog (4)
Apache (2)
Charities (2)
Coldfusion (7)
Dreams (3)
House Stuff (2)
Landscaping And Gardening (3)
Linux (3)
Movies/TV (2)
MySQL (6)
Past Memories (1)
Perl (1)
Perspectives and Understandings (3)
Pets and Animals (2)
Politics (1)
Random Ramblings (9)
Recipes (5)
Software Development - General (3)
Spiritual Awakenings (2)
Sports and Fitness (2)
Travel (47)
Volunteer Work (5)





The content and programming on this site is © Copyright Carlos Vazquez 2006, All Rights Reserved.
Website Design obtained from Steve`s Web templates