jandrews3 Posted January 17, 2010 Share Posted January 17, 2010 I need to be able to remove "Yo ", "yo ", "Tú ", "tú ", "Nosotros ", "nosotros " from a string and then test the string against the correct answer in my database (I'm a teacher). I thought the following would work: $remove = array("Yo ", "yo ", "Tú ", "tú ", "Nosotros ", "nosotros "); if (ucfirst(str_replace($remove,"",$ans1)) == $dba1) {$stat1 = 1; $correct++;} else {$missed++; $stat1 = 0;} The ucfirst is to make sure the first letter of the string is capitalized in case the removed word had been first. It works for Yo, yo, Nosotros and nosotros; but not for Tú or tú. The reason appears to be that the str_replace doesn't recognize that the user input "Tú" is the same as the $remove variable's "Tú" value. Is there a fix to this. Thanks. I also tried it this way without any success: $remove = array("Yo ", "yo ", "Tú ", "tú ", "Nosotros ", "nosotros "); Link to comment https://forums.phpfreaks.com/topic/188790-weird-str_replace-need/ Share on other sites More sharing options...
jandrews3 Posted January 17, 2010 Author Share Posted January 17, 2010 A quicker explanation might be ... why doesn't str_replace("Tú ","",$ans1) delete the "Tú " from the string "Tú quieres ir?" Link to comment https://forums.phpfreaks.com/topic/188790-weird-str_replace-need/#findComment-996698 Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 What sort of encoding are you using? Link to comment https://forums.phpfreaks.com/topic/188790-weird-str_replace-need/#findComment-996701 Share on other sites More sharing options...
jandrews3 Posted January 17, 2010 Author Share Posted January 17, 2010 It's a regular form posting from quiz_take.php to quiz_grade.php page. I need the str_replace to remove occurrences of "Yo ", "yo ", "Tú ", "tú ", "Nosotros ", "nosotros ". I can get it to work with all but "Tú " and "tú ". As far as encoding goes, I'm not sure what you mean. I'm still pretty much an amateur. It's a simple form submission. I tried the str_replace with "Tú " and "tú " but to no avail. Link to comment https://forums.phpfreaks.com/topic/188790-weird-str_replace-need/#findComment-996704 Share on other sites More sharing options...
jandrews3 Posted January 17, 2010 Author Share Posted January 17, 2010 Looks like the solution might be htmlentities convert the user input data first before comparing it. Thanks. I wouldn't have gone down that path without your encoding quesiton. Link to comment https://forums.phpfreaks.com/topic/188790-weird-str_replace-need/#findComment-996719 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.