Odd Single Quotes Issues

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
buywithcredit
Forum Newbie
Posts: 1
Joined: Fri Sep 18, 2009 1:40 am

Odd Single Quotes Issues

Post by buywithcredit »

For some reason, people who submit data with this type of single quote (’ -- Type A) causes premature truncation in my database insert statement while PHP's internal magic quotes will escape this type of single quote (' -- Type B). My question is how can replace Type A with Type B using preg_replace. I can't seem to save code to find that particular quote, because of encoding issues. What am I to do? How can I detect that type of quote is being submitted? Thanks.
User avatar
turbolemon
Forum Commoner
Posts: 70
Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK

Re: Odd Single Quotes Issues

Post by turbolemon »

You could try this

Code: Select all

$codes = array (
"\x2019" => "'",
"\xE2\x80\x98", => "'" //Example multi-byte / multi-character replacement
);
$output = strtr($source,$codes);
This will replace all characters which code is 2019 (RIGHT SINGLE QUOTATION MARK?) with the ascii single quote character. You can also add multi-byte character strings to the codes array, as is shown in the second member of the $codes array. Using the code representation gets round your encoding issue.

Also, you can add any other characters you wish to replace to the $codes associative array. I use the Firefox Add-on "Character Identifier", which provides a context menu item that allows you to select characters in a document and identify each character code. https://addons.mozilla.org/en-US/firefox/addon/3929. It shows Unicode characters as U+xxx, the code you require being U+2019.

Hope that helps!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Odd Single Quotes Issues

Post by jackpf »

Surely if you just run the input through your database's escape string function, all kinds of quotes would be taken care of?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Odd Single Quotes Issues

Post by Ollie Saunders »

For some reason, people who submit data with this type of single quote (’ -- Type A) causes premature truncation in my database insert statement
What database are you using? What character set is the table/field you're inserting into configured to accept?

If you're using MySQL, disable magic_quotes and escape quotes manually using mysql_real_escape_string().
Post Reply