Strange PHP/textbox/special character/apostrophe problem I can't figure out.

Hi all,
I have a problem. I have two different pages. Both of them have identical textboxes displaying a variable with special characters in it. One of them displays the special characters correctly, the other one does not. I can’t figure out why.

The value of the variable is “Tester’s Event”, without the double quotes. There is an apostrophe in it.
The line that displays the variable looks like this:
[php]

[/php]

This variable $name is a $_POST variable. The variable initially looks like this:
Tester%27s%20Event - the special characters are my clumsy attempt at stopping apostrophes causing problems.
Before the variable is used in a database query, the following lines are applied to it:
[php]
$name = urldecode($name);
$name = str_replace("’", “’’”, $name);
[/php]

After the query, I would like to display it in my textbox. To prevent errors, I run the following two lines:
[php]
$name = str_replace("’’", “’”, $name);
$name = htmlentities($name, ENT_QUOTES);
[/php]

So in my first page, this line displays my variable no problems. Special characters are converted correctly.
[php]

[/php]

BUT! On my second page, I have an identical logical setup. Special characters are not converting, and the variable is displaying like: Tester's Event and I have no idea why.

The code looks like this:
[php]

[/php]

I tried hard-coding the variable, like this:
[php]
$name = ‘Tester & # 3 9 ; s Event’; (without the spaces in the first word)
[/php]
In that case, the special character converted fine and displayed as ‘Tester’s Event’.

Thanks for your help. I hope I’ve been clear enough.
James.

I wish I can offer some advice here, but I’m at a loss…

I wonder if there’s something else going on that I’m not seeing in the examples you provided.

I tried and the code provided works like a charm.

You’re introducing all kinds of craziness! Are you using MS SQL? That is the only DB that I know of that escapes a ’ with a '. If MySQL just use mysqli_real_escape_string() before inserting into the database and htmlentities() when echoing it. Get rid of all the hard coding, str_replace(), urldecode() etc… That mess is probably the cause of your problems.

Sponsor our Newsletter | Privacy Policy | Terms of Service