icekat83 Posted September 25, 2009 Share Posted September 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/175519-weird-formatted-apostrophe-problem/ Share on other sites More sharing options...
thebadbad Posted September 25, 2009 Share Posted September 25, 2009 You're sure they're not ´s instead of ’s? Else post some sample code. Quote Link to comment https://forums.phpfreaks.com/topic/175519-weird-formatted-apostrophe-problem/#findComment-924872 Share on other sites More sharing options...
eatfishy Posted September 25, 2009 Share Posted September 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/175519-weird-formatted-apostrophe-problem/#findComment-925022 Share on other sites More sharing options...
thebadbad Posted September 26, 2009 Share Posted September 26, 2009 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"]. Quote Link to comment https://forums.phpfreaks.com/topic/175519-weird-formatted-apostrophe-problem/#findComment-925156 Share on other sites More sharing options...
redarrow Posted September 26, 2009 Share Posted September 26, 2009 There's no difference between $_POST['string'] and $_POST["string"]. Must be a preference php programmers choice again? Quote Link to comment https://forums.phpfreaks.com/topic/175519-weird-formatted-apostrophe-problem/#findComment-925158 Share on other sites More sharing options...
thebadbad Posted September 26, 2009 Share Posted September 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/175519-weird-formatted-apostrophe-problem/#findComment-925162 Share on other sites More sharing options...
redarrow Posted September 26, 2009 Share Posted September 26, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175519-weird-formatted-apostrophe-problem/#findComment-925164 Share on other sites More sharing options...
thebadbad Posted September 26, 2009 Share Posted September 26, 2009 What? You lost me there. Quote Link to comment https://forums.phpfreaks.com/topic/175519-weird-formatted-apostrophe-problem/#findComment-925291 Share on other sites More sharing options...
Daniel0 Posted September 26, 2009 Share Posted September 26, 2009 Neither is better. Quote Link to comment https://forums.phpfreaks.com/topic/175519-weird-formatted-apostrophe-problem/#findComment-925292 Share on other sites More sharing options...
Aeolus Posted September 26, 2009 Share Posted September 26, 2009 Is this a recognized character type? Try formatting it after it comes out of the database instead of before it goes in - see how that works for you. Quote Link to comment https://forums.phpfreaks.com/topic/175519-weird-formatted-apostrophe-problem/#findComment-925397 Share on other sites More sharing options...
icekat83 Posted October 8, 2009 Author Share Posted October 8, 2009 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 ' 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. Quote Link to comment https://forums.phpfreaks.com/topic/175519-weird-formatted-apostrophe-problem/#findComment-932832 Share on other sites More sharing options...
thebadbad Posted October 8, 2009 Share Posted October 8, 2009 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 ' 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(''', "'", $str); or by using html_entity_decode(), but that will convert all HTML entities to the characters they represent. Quote Link to comment https://forums.phpfreaks.com/topic/175519-weird-formatted-apostrophe-problem/#findComment-932857 Share on other sites More sharing options...
icekat83 Posted October 8, 2009 Author Share Posted October 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175519-weird-formatted-apostrophe-problem/#findComment-932959 Share on other sites More sharing options...
cags Posted October 8, 2009 Share Posted October 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/175519-weird-formatted-apostrophe-problem/#findComment-932968 Share on other sites More sharing options...
thebadbad Posted October 8, 2009 Share Posted October 8, 2009 Could also have something to do with the character encoding. If you post a file including a sample string with the odd characters, we can test your code and see our results. Quote Link to comment https://forums.phpfreaks.com/topic/175519-weird-formatted-apostrophe-problem/#findComment-932978 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.