eugeniu Posted September 4, 2009 Share Posted September 4, 2009 I wrote a script that retrieves a word from a database made up of Japanese characters, adds two more Japanese characters to the script, and has the user enter the new word, in Japanese. For the purpose of debugging, I've simplified the script. The script is supposed to take the submitted word and check if it matches the word from the database, and return "Success" or "Fail". However, no matter how many times I enter the correct word, it determines it's incorrect. The code to the script is shown below: <?php session_start(); $title = "Test Quiz"; $autoselect = "quiz.answer"; include ("header.php"); echo "<h1>Test Quiz</h1><div id=\"quiz\">"; $entry = mysql_query("SELECT * FROM verbs LIMIT 1") or die(mysql_error()); $entry = mysql_fetch_array($entry); $masu = "ます"; echo "Answer: $entry[ans_kana] + $masu<br />"; if (isset($_POST['form_submit'])) { $answer = $_POST['answer']; $rightanswer = $entry['ans_kana']; $rightanswer .= $masu; if ($answer == $rightanswer) { echo "Sucess.<br />$answer = $rightanswer<br />"; } else { echo "Fail.<br />$answer =/ $rightanswer<br />"; } } ?> <form name="test" action="test.php" method="post"> <input id="answer" type="text" name="answer" size="10" maxlength="60" /> <input type="hidden" name="form_submit" value="submitted" /> <input type="submit" name="submit" value=">>" /> </form> <?php echo "</div>"; include("footer.php"); ?> The above script can be run at http://kanaquest.com/dev/test.php . The right answer is supposed to be たべます, which is a combination of the string from the database, and $masu. But it seems that when a string from the database is combined with a UTF-8 string, nothing I enter matches the formed string. However, when $rightanswer is only a word from the database, or only a UTF-8 string set in the script, I have much better luck with the script matching up my submitted word with the word in the script. What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/173128-strange-problem-with-using-japanese-characters/ Share on other sites More sharing options...
SilveR316 Posted September 4, 2009 Share Posted September 4, 2009 What character encoding is the database set to store? Unless its UTF-8, you can't compare the strings directly. Quote Link to comment https://forums.phpfreaks.com/topic/173128-strange-problem-with-using-japanese-characters/#findComment-912568 Share on other sites More sharing options...
eugeniu Posted September 4, 2009 Author Share Posted September 4, 2009 It's listed as utf8_general_ci. And if it means anything, I have this in my header: header("Content-type: text/html; charset=utf-8"); mb_internal_encoding("UTF-8"); Quote Link to comment https://forums.phpfreaks.com/topic/173128-strange-problem-with-using-japanese-characters/#findComment-912571 Share on other sites More sharing options...
SilveR316 Posted September 4, 2009 Share Posted September 4, 2009 What happens when you var_dump() both the input value and the value from the database? Do they both report being the same length? As a matter of fact, you're appending html character entities as apposed to regular characters. This is probably why they don't match. Try converting the input with htmlentities(). Quote Link to comment https://forums.phpfreaks.com/topic/173128-strange-problem-with-using-japanese-characters/#findComment-912578 Share on other sites More sharing options...
eugeniu Posted September 4, 2009 Author Share Posted September 4, 2009 string(12) "たべます" string(22) "たべます" Guess not. I tried using htmlentities on the input string, then both strings, but I still get the same problem. Answer: たべ + ます string(12) "たべます" string(22) "たべます" htmlentities()... string(12) "たべます" string(22) "たべます" Fail. たべます =/ たべます Quote Link to comment https://forums.phpfreaks.com/topic/173128-strange-problem-with-using-japanese-characters/#findComment-912582 Share on other sites More sharing options...
newbtophp Posted September 4, 2009 Share Posted September 4, 2009 Kornichiwa, Have you tried? htmlspecialchars Quote Link to comment https://forums.phpfreaks.com/topic/173128-strange-problem-with-using-japanese-characters/#findComment-912589 Share on other sites More sharing options...
eugeniu Posted September 4, 2009 Author Share Posted September 4, 2009 Sadly, using that one doesn't seem to make a difference. Quote Link to comment https://forums.phpfreaks.com/topic/173128-strange-problem-with-using-japanese-characters/#findComment-912591 Share on other sites More sharing options...
SilveR316 Posted September 4, 2009 Share Posted September 4, 2009 Theyre different lengths because they are different encodings. Try using iconv() or mb_convert_encoding() to convert them to the same encoding. Quote Link to comment https://forums.phpfreaks.com/topic/173128-strange-problem-with-using-japanese-characters/#findComment-912592 Share on other sites More sharing options...
eugeniu Posted September 4, 2009 Author Share Posted September 4, 2009 I tried doing iconv("UTF-8", "ISO-8859-1//TRANSLIT", $rightanswer);, but that's not doing anything. Is there something else I'm supposed to use for the first two properties? Quote Link to comment https://forums.phpfreaks.com/topic/173128-strange-problem-with-using-japanese-characters/#findComment-912698 Share on other sites More sharing options...
eugeniu Posted September 4, 2009 Author Share Posted September 4, 2009 I also tried doing the following: mb_convert_encoding($answer, "ISO-8859-1", "UTF-8"); mb_convert_encoding($rightanswer, "ISO-8859-1", "UTF-8"); But that doesn't work either! Quote Link to comment https://forums.phpfreaks.com/topic/173128-strange-problem-with-using-japanese-characters/#findComment-912742 Share on other sites More sharing options...
eugeniu Posted September 4, 2009 Author Share Posted September 4, 2009 Sigh, I gave up trying to do this the way I wanted to and just created a mysql table that has the various word endings I need in romaji (Japanese using latin alphabet) and in kana (Japanese), where I can request something using romaji and get it in kana. Quote Link to comment https://forums.phpfreaks.com/topic/173128-strange-problem-with-using-japanese-characters/#findComment-912759 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.