Swapping quotation marks for apostrophes

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
Leao
Forum Commoner
Posts: 49
Joined: Mon Aug 21, 2006 8:57 pm
Location: London

Swapping quotation marks for apostrophes

Post by Leao »

Hi,

I've got a form that sends text to the attached PHP script that subsequently submits the text to a mySQL database. I'd like to convert all the right single quotation marks featured within the text into apostrophes '. I've experimented with the attached code but it doesn't seem to work. I've also tried swapping the characters' HTML equivalents in the same fashion but it didn't work either.

Any idea what I'm doing wrong??

Many, many thanks,

a grateful leao

Code: Select all

<?php
mysql_connect("mysqlserver", "user", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$leaostext = mysql_real_escape_string($_POST['leaostext']);

$leaostext = str_replace("’","'",$leaostext);

$result = mysql_query("UPDATE table SET leaostext = '$leaostext'") or die(mysql_error());
?>
Last edited by Leao on Wed Oct 18, 2006 3:44 am, edited 1 time in total.
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post by choppsta »

The variable $leaostext doesn't exist. Set the PHP warning level to ALL and you'd get a notice error that would alert you to this.
Leao
Forum Commoner
Posts: 49
Joined: Mon Aug 21, 2006 8:57 pm
Location: London

Post by Leao »

choppsta wrote:The variable $leaostext doesn't exist. Set the PHP warning level to ALL and you'd get a notice error that would alert you to this.
Sorry, I made a mistake when writing the code out on the forum. I've corrected the example above – any ideas why it still isn't working?

Leao
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post by choppsta »

It could be because you're escaping the string with mysql_real_escape_string() and then replacing the single quote marks. This will cause an error in your SQL statement as you'll have unescaped quotes in there:

Code: Select all

e.g. SET leoastext='some text, isn't it nice.'
I think if you swap these round so you do the replace first it should solve your problem.
Leao
Forum Commoner
Posts: 49
Joined: Mon Aug 21, 2006 8:57 pm
Location: London

Post by Leao »

choppsta wrote:It could be because you're escaping the string with mysql_real_escape_string() and then replacing the single quote marks. This will cause an error in your SQL statement as you'll have unescaped quotes in there:

Code: Select all

e.g. SET leoastext='some text, isn't it nice.'
I think if you swap these round so you do the replace first it should solve your problem.
I've tried doing the following, is this what you meant? It still fails to swap the characters. I think it might be something to do with the particular characters I'm trying to swap. For example if I try to swap all instances of the word "cat" into the word "dog" it does work.

Thanks again,

leao

Code: Select all

<?php
mysql_connect("mysqlserver", "user", "password") or die(mysql_error()); 
mysql_select_db("database") or die(mysql_error());

$leaostext = mysql_real_escape_string(str_replace("’","'",$_POST['leaostext']));

$result = mysql_query("UPDATE leaostable SET leaostext = '$leaostext'") or die(mysql_error()); 
?>
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Have you tried echo'ing $leaostext, or your final query to see the what the output is?
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post by choppsta »

Yep, that's correct.

Sounds like what you think is a single quote mark is actually another character. Have you tried copy and pasting the character you want to replace into your code? Depending on what editor you're using it might be worth a shot?
Leao
Forum Commoner
Posts: 49
Joined: Mon Aug 21, 2006 8:57 pm
Location: London

Post by Leao »

Jenk wrote:Have you tried echo'ing $leaostext, or your final query to see the what the output is?
The output is the text $leaostext with the right single quotation marks still present.
choppsta wrote:Sounds like what you think is a single quote mark is actually another character. Have you tried copy and pasting the character you want to replace into your code? Depending on what editor you're using it might be worth a shot?
I've being copying and pasting the character I want to replace into my code from the beginning, I've also tried using the character's HTML equivalent: & #8217; (N.B. I separated '&' from '#' in this example so that the forum wouldn't translate the code immediately into its equivalent character.)

Any other ideas?

Thanks,

Leao
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post by choppsta »

Are you copy and pasting from MS Word?

If so, there's a couple of comments on the str_replace() manual page about replacing "smart quotes" that might help:
http://uk2.php.net/str_replace

Just found this too:
http://shiflett.org/archive/165
Post Reply