son.of.the.morning Posted September 15, 2010 Share Posted September 15, 2010 Is it possible for php the check for individual letters in a sql record. For example is the word 'superman' was stored in sql record and my page told the user to input the first, fourth and last letter of the word into a text box would php be able to check the record to see if the letter exists the word and that they are in the correct order? Quote Link to comment https://forums.phpfreaks.com/topic/213501-possible-with-php-and-mysql/ Share on other sites More sharing options...
systemick Posted September 15, 2010 Share Posted September 15, 2010 You would need to pull the word out of the database then test it for the letters. If you had asked for letters 1,3&4 you would do somehting like this if ($secret_word[0] == 's' && $secret_word[2] == 'p' && $secret_word[3] == 'e') { <Do something> } Quote Link to comment https://forums.phpfreaks.com/topic/213501-possible-with-php-and-mysql/#findComment-1111416 Share on other sites More sharing options...
son.of.the.morning Posted September 15, 2010 Author Share Posted September 15, 2010 Wouldn’t the word have to be broken up, possible into an array. I don’t no if i am making much sense of the code below, [0],[1],[2] are these referring the individual characters in the record? Quote Link to comment https://forums.phpfreaks.com/topic/213501-possible-with-php-and-mysql/#findComment-1111419 Share on other sites More sharing options...
systemick Posted September 15, 2010 Share Posted September 15, 2010 You can treat a string as an array in php. Try this $word = 'superman'; print 'first letter = '.$word[0]; Quote Link to comment https://forums.phpfreaks.com/topic/213501-possible-with-php-and-mysql/#findComment-1111420 Share on other sites More sharing options...
fortnox007 Posted September 15, 2010 Share Posted September 15, 2010 can't it be done with preg_match? like: $string ='superman'; echo preg_match('/(superman)/','$string');// will output 1 (true) if the first part is in the string Quote Link to comment https://forums.phpfreaks.com/topic/213501-possible-with-php-and-mysql/#findComment-1111421 Share on other sites More sharing options...
mraza Posted September 15, 2010 Share Posted September 15, 2010 you can also check preg-split <?php $str = 'string'; $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); print_r($chars); ?> Quote Link to comment https://forums.phpfreaks.com/topic/213501-possible-with-php-and-mysql/#findComment-1111423 Share on other sites More sharing options...
PFMaBiSmAd Posted September 15, 2010 Share Posted September 15, 2010 Since this word in stored in a database, you would generally want to do this in a query and check if the query matched any row(s) - $first, $fourth, and $last php variables hold the entered characters. The underscore _ wildcard character ignores the 2nd, 3rd, 5th, 6th, and 7th positions - $query = "SELECT COUNT(*) FROM your_table WHERE your_column LIKE '$first__$fourth___$last' AND another_condition_that_identifies_the_specific_word_you_want_to_check" Quote Link to comment https://forums.phpfreaks.com/topic/213501-possible-with-php-and-mysql/#findComment-1111427 Share on other sites More sharing options...
son.of.the.morning Posted September 15, 2010 Author Share Posted September 15, 2010 i want to write a code, which will ask for different letters every time and once the user has submitted the form with the text box in it will then check to see if the letters are correct and in the right order from what the first page had indicated to the user. Quote Link to comment https://forums.phpfreaks.com/topic/213501-possible-with-php-and-mysql/#findComment-1111429 Share on other sites More sharing options...
PFMaBiSmAd Posted September 15, 2010 Share Posted September 15, 2010 Then just write code to do that. You could build the LIKE '....' term dynamically with the entered values and wildcard characters in any positions you want. Quote Link to comment https://forums.phpfreaks.com/topic/213501-possible-with-php-and-mysql/#findComment-1111432 Share on other sites More sharing options...
son.of.the.morning Posted September 15, 2010 Author Share Posted September 15, 2010 is there away i can make an array of possible combinations for the word? Quote Link to comment https://forums.phpfreaks.com/topic/213501-possible-with-php-and-mysql/#findComment-1111443 Share on other sites More sharing options...
son.of.the.morning Posted September 15, 2010 Author Share Posted September 15, 2010 I have just read a short tutorial on wildcard in php, i cant access my ftp at the moe so i cant try the code. Dont no if this will work, does this seem right? $word = superman $match = $word[1].$word[2].$word[5]; if ($pWord == $match) { echo "ya mar"; } Quote Link to comment https://forums.phpfreaks.com/topic/213501-possible-with-php-and-mysql/#findComment-1111447 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.