firelight4321 Posted March 10, 2008 Share Posted March 10, 2008 The text game I want to create is going to follow closely along the lines of the old text game called ZORK. The problem I'm having, is that I have this great design, and still can't figure out how to implement it. Does anyone know of a way to take the text the Player inputs, have my php script read it, and then output the correct text? I've looked at examples for superglobal arrays: $_REQUEST, $_POST, and $_GET. But, all the examples that are given just take the information the Player inputs and outputs it in a variables place. I need the Player's input text to be read by my script and matched, or not matched, accordingly and give an output based on what they wrote. By the way, I'm new to PHP. It's having a hard time growing on me. HELP?!?! ??? :'( Link to comment https://forums.phpfreaks.com/topic/95520-trying-to-create-a-text-game/ Share on other sites More sharing options...
Barand Posted March 10, 2008 Share Posted March 10, 2008 By the way, I'm new to ZORK so what do you mean by Does anyone know of a way to take the text the Player inputs, have my php script read it, and then output the correct text? Link to comment https://forums.phpfreaks.com/topic/95520-trying-to-create-a-text-game/#findComment-488987 Share on other sites More sharing options...
trq Posted March 10, 2008 Share Posted March 10, 2008 As a simple example. <?php if (isset($_POST['submit'])) { switch ($_POST['text'])) { case 'hello': echo "Hello!!!"; break; case 'foo': echo "What the hell is foo?"; break; default: echo "Invalid command"; } } ?> <form action="" method="post"> <input type="text" name="text"> (type foo or hello) <input type="submit" name="submit"> </form> Link to comment https://forums.phpfreaks.com/topic/95520-trying-to-create-a-text-game/#findComment-488990 Share on other sites More sharing options...
firelight4321 Posted March 10, 2008 Author Share Posted March 10, 2008 Thank you Thorpe!!! That was extremely helpful. I didn't know about the 'case' function. Link to comment https://forums.phpfreaks.com/topic/95520-trying-to-create-a-text-game/#findComment-488993 Share on other sites More sharing options...
discomatt Posted March 10, 2008 Share Posted March 10, 2008 Does anyone know of a way to take the text the Player inputs, have my php script read it, and then output the correct text? Regular Expressions (REGEX) might be of interest to you... if you wanted commands like /pickup item name and you had a text element <input type="text" name="input" /> your script would look something like this: <?php $items = array('robe', 'wizard hat'); if (preg_match('=^/pickup (.+)$=i', $_POST['input'], $match) ) { if (in_array(strtolower($match[1]), $items) ) echo 'You have picked up a ' . ucwords($match[1]); else echo 'Could not find ' . ucwords($match[1]); } else { echo 'Unknown command'; } ?> That should give you a bit of a start... study up on those functions and regular expressions. Good luck with the game Link to comment https://forums.phpfreaks.com/topic/95520-trying-to-create-a-text-game/#findComment-488996 Share on other sites More sharing options...
firelight4321 Posted March 10, 2008 Author Share Posted March 10, 2008 Thank you discomatt, I appreciate it, like, loooots. <333 Link to comment https://forums.phpfreaks.com/topic/95520-trying-to-create-a-text-game/#findComment-489004 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.