parabolate Posted October 8, 2009 Share Posted October 8, 2009 Hey all, I am new to this forum and brand new to php, never worked with it before. I am trying to write a script that takes in 5 words from the user via a text box and then prints them out in reverse order. Also, I want to validate the input to make sure that only letters were provided as input - no numbers or characters (i.e. 1 2 3 & ! # + \ etc.). So, if one two three four five was entered.....five four three two one would be printed. Anyone have any tips they could offer a newb as to how to accomplish this? I've been browsing around the web for awhile now and haven't found anything that helps me out - everything I've come across thus far has been for more advanced scripts Hoping one of you knowledgeable folk can give me some more effective input! Thanks! Link to comment https://forums.phpfreaks.com/topic/176903-solved-help-with-a-basic-php-script/ Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 the string reverse is pretty easy, just use the strrev function. $string = "Hello World!"; $reverse = strrev($string); As for the validation, you can accomplish that with regex. its a very simple regex expression match, but i'm awful with regex, so I don't want to give you something that is wrong Link to comment https://forums.phpfreaks.com/topic/176903-solved-help-with-a-basic-php-script/#findComment-932734 Share on other sites More sharing options...
Michdd Posted October 8, 2009 Share Posted October 8, 2009 <?php //Store the value of the form element that has the name of "input" in a variable named $input $input = $_POST['input']; //Check to see if the string contains only letters (spaces are also allowed) if(ctype_alpha(str_replace(' ', NULL, $input))) { //Split the string into an array of the different words //Then reverse the order of the words in the array //Finally reform the string separating each word with a space and echoing it echo implode(' ', array_reverse(explode(' ', $input))); } else { //They entered non-alpha characters echo 'You entered Invalid characters'; } ?> Note: That doesn't check to make sure exactly 5 words were entered. But that would be easy to check for. Link to comment https://forums.phpfreaks.com/topic/176903-solved-help-with-a-basic-php-script/#findComment-932735 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.