Jump to content

Weird Formatted Apostrophe Problem


icekat83

Recommended Posts

Hi,

 

I have a really small and kind of embarrassing problem but one that is really annoying. I have some text being put in to a database however because the text is likely to be copy and pasted I want to account for formatted apostrophes. ’ instead of '. (see how one is formatted)

 

Anyway I've done a really simple str_replace("’", "'", $string) which works fine except when using $_POST['string']. I have no idea why and can't find a solution. Does anyone know of a problematic history with formatted apostrophes?

 

Originally the string was really long and coming from a form but when I replaced it with a single line coming from the form it still doesn't work. At the moment the string is being put in the database with funny characters because it doesn't know what to do with the formatted apostrophe.

 

If anyone can help please let me know. I should have been able to solve this but can't figure it out so any help is appreciated.

 

Thanks,

Alanna.  :)

 

Link to comment
Share on other sites

str_replace("’", "'", $string) which works fine except when using $_POST['string'].

 

Did you try to change  $_POST['string'] to  $_POST["string"] ? Incase the text encoding for that apostropes is read as a single quotation, using double quotation will not distort your string in your example.

Link to comment
Share on other sites

str_replace("’", "'", $string) which works fine except when using $_POST['string'].

 

Did you try to change  $_POST['string'] to  $_POST["string"] ? Incase the text encoding for that apostropes is read as a single quotation, using double quotation will not distort your string in your example.

 

There's no difference between $_POST['string'] and $_POST["string"].

Link to comment
Share on other sites

 

 

There's no difference between $_POST['string'] and $_POST["string"].

 

Must be a preference php programmers choice again?

 

I don't know if I'm interpreting that right, but if I am, I'll say that it shouldn't be about preference. Only use double quotes when you want to use variables directly inside the string. And yes, this is off topic.

Link to comment
Share on other sites

Off topic....

 

so it a augmentative solution.

 

$_POST['redaarow'] <<<< correct way

 

$_POST["redaarow"] <<<< also correct way

 

but first solution should be used, if you no php as a professional or general user.

 

That still contradicts the fact both can be used, it not slower or faster lol.

 

This could even be turned into a programming debate, as setting the $_POST[] variable with " double or ' singe quotes do the same as each other while posting a set variable.

 

 

 

I don't know if I'm interpreting that right, but if I am, I'll say that it shouldn't be about preference. Only use double quotes when you want to use variables directly inside the string. And yes, this is off topic.

 

WANT TO NO WHAT BETTER THEN?

 

DOUBLE QUOTES OR SINGLE QUOTES SET FOR $_post[] VARIABLES?

 

$_POST['redaarow'] <<<< THIS WAY

 

$_POST["redaarow"] <<<< OR THIS WAY

Link to comment
Share on other sites

  • 2 weeks later...

Hi guys,

 

Thanks for the replies. I have a bit more info on this strange mess I have.

 

First of all I had a look at thebadbad's suggestion and found that the apostrophe is either a ´ or a ’

I suspect it's the second version.

 

I have the posted stuff being run through the following function:

 

function edit_text($str){

$old[1] = "'";

$old[2] = "’";

$old[3] = "´";

$new[1] = "?";

$new[2] = "?";

$new[3] = "?";

$new_str = str_replace($old, $new, $str);

return $new_str;

}

 

** I AM AWARE that at the moment all apostrophes are being replaced by a question mark. This was done deliberately and showed that whilst normal instances of apostrophes (number 1) are being replaced normally, the others are not changed at all.

 

When added to the database the unchanged apostrophes end up as either:

          OR          ’

 

I have two final questions.

1. Does slashes effect the function I've used above?

2. Does it matter what I'm copying and pasting. ie if I were to copy and paste text which had an apostrophe written as &#39; would that effect how my function handles it?

 

 

Anyway thanks to anyone who can help me work this one out. I have NO idea what I going on and this is positively the weirdest problem I've ever had.

 

Alanna.

 

 

Link to comment
Share on other sites

1. Does slashes effect the function I've used above?

2. Does it matter what I'm copying and pasting. ie if I were to copy and paste text which had an apostrophe written as &#39; would that effect how my function handles it?

 

1. No.

2. If your apostrophes are represented with HTML entities, that's where your problem lies. When you output your sample string, check the source code of it, and see if you find any HTML entities. If you do, you can simply replace them with something like

 

$str = str_replace('&#39;', "'", $str);

 

or by using html_entity_decode(), but that will convert all HTML entities to the characters they represent.

Link to comment
Share on other sites

Thanks for the reply thebadbad

 

That's the strange part. When I view the straight mysql, without any formatting the apostrophe is not formatted that way. To add to the mystery the second kind of apostrophe ( ’ ) is converted (where the third which is: ´ and not converted at all, and the one I need converted) however the second version, when converted, is changed in to an apostrophe but it almost acts like the converted version is added to it because I still get the "Â" as well as the converted result - Â'

 

What does everyone else get when using my function? Do they get the same strange results or is my result unique?

 

Finally to add to this mystery - html_entity_encode() did nothing. htmlentities() resulted in:

  1 - \'

  2 - ´

  3 - â�� (those question marks usually turn in to something like the 'Â' when inserted in to the sql table)

 

 

This is so weird and has me totally stunned. Is there some other way I can remove the special formatting of text (like what MSWord puts in)? I suspect that might help.

 

Thanks for the replies everyone! I am very grateful for the help.

 

Alanna

Link to comment
Share on other sites

I had a similar issue recently. I was allowing a user to copy a table from Word, paste it into a textarea and it would then be converted from a tab delimited table into a HTML table and stored in the database. I couldn't find out for the life of me what the ascii value of the character was. I simply fixed it by copying one I knew didn't work from the database using PHPMyAdmin and using str_replace to switch it for a character I knew worked. By the look of what you've tested, this might not help. But one of the things I tried when testing was.

 

$input = "the value you wish to check for the characters";

echo htmlentities($input) . '<br/>';
echo urlencode($input). '<br/>';

 

This shoud help you calculate exactly what character is causing the problem.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.