woody79 Posted June 10, 2007 Share Posted June 10, 2007 An Example: I have a string that contains a sentence. The user inputs what they think the sentence might be about and php returns whether they were correct or incorrect. Is there a function that will do that. It is a bit like selecting something from a database using the mysql_query("select * from myTable where name like '%W%'");? Link to comment https://forums.phpfreaks.com/topic/54936-solved-php-string-help/ Share on other sites More sharing options...
christofurr Posted June 10, 2007 Share Posted June 10, 2007 Are the sentence strings stored in a database? Link to comment https://forums.phpfreaks.com/topic/54936-solved-php-string-help/#findComment-271681 Share on other sites More sharing options...
woody79 Posted June 10, 2007 Author Share Posted June 10, 2007 No a variable, like so $str = "The dog's name was Rover"; Link to comment https://forums.phpfreaks.com/topic/54936-solved-php-string-help/#findComment-271683 Share on other sites More sharing options...
christofurr Posted June 10, 2007 Share Posted June 10, 2007 Split the sentences into words, assign each to it's own variable, and use the ereg function. <html> <body> <!-- Guess Input --> <form action="sentence.php" method="post"> What's the sentence about? <input type="text" name="guess" /> <input type="submit" /> </form </body> </html> <?php /*Sentence.php*/ $str1 = "The"; $str2 = "dog"; $str3 = "name"; $str4 = "was"; $str5 = "Rover"; $str6 = "The dog's name was Rover."; if (!ereg('($str1)+', $_POST["guess"])) && (!ereg('($str2)+', $_POST["guess"])) && (!ereg('($str3)+', $_POST["guess"])) && (!ereg('($str3)+', $_POST["guess"])) && (!ereg('($str5)+', $_POST["guess"])) { echo "You're completely off."; } else { echo "Good guess! The actual sentence was " . "'" . $str6 . "'"; } ?> Link to comment https://forums.phpfreaks.com/topic/54936-solved-php-string-help/#findComment-271684 Share on other sites More sharing options...
christofurr Posted June 10, 2007 Share Posted June 10, 2007 You can also configure it a bit more for the script to tell the user how many and which words matched. Link to comment https://forums.phpfreaks.com/topic/54936-solved-php-string-help/#findComment-271685 Share on other sites More sharing options...
chigley Posted June 10, 2007 Share Posted June 10, 2007 You could always just convert the solution and answer to lower case, then use strpos()... Link to comment https://forums.phpfreaks.com/topic/54936-solved-php-string-help/#findComment-271702 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.