Jump to content

[SOLVED] PHP String Help


woody79

Recommended Posts

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

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 . "'";
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.