simonjk Posted September 18, 2012 Share Posted September 18, 2012 Hi, This is perhaps a bit of a simple one, but I am not sure whether it is a preg_match or strpos that I should use. I have a form which is completed by the user. I want to check that in one of the form field entries the word they enter begins with E, L or B I thought it might be built as a function, something like function checkWord($word){ return preg_match('/^E)(/^L)(/^B/'), $word) ? TRUE : FALSE; Sorry if my attempt is laughable, can't seem to work this one out? Any help appreciated. Simon Link to comment https://forums.phpfreaks.com/topic/268502-check-that-word-begins-with-specific-letters/ Share on other sites More sharing options...
gristoi Posted September 18, 2012 Share Posted September 18, 2012 function checkWord($word) { $firstLetter = substr($word,0,1); $requiredLetters = array('E','L','B'); if(!in_array($firstLetter, $requiredLetters)){ return false; } return true; } hope that helps Link to comment https://forums.phpfreaks.com/topic/268502-check-that-word-begins-with-specific-letters/#findComment-1378854 Share on other sites More sharing options...
Barand Posted September 18, 2012 Share Posted September 18, 2012 or function CheckWord($word) { return in_array($word[0], array('E', 'L', 'B')); } echo CheckWord('Ewok') ? 'Yes' : 'No'; // Yes echo CheckWord('word') ? 'Yes' : 'No'; // No Link to comment https://forums.phpfreaks.com/topic/268502-check-that-word-begins-with-specific-letters/#findComment-1378857 Share on other sites More sharing options...
spiderwell Posted September 18, 2012 Share Posted September 18, 2012 i prefer the second example only for the word ewok!! Link to comment https://forums.phpfreaks.com/topic/268502-check-that-word-begins-with-specific-letters/#findComment-1378858 Share on other sites More sharing options...
cyberRobot Posted September 18, 2012 Share Posted September 18, 2012 I would suggest adding strtoupper() to make the solution case insensitive: <?php function CheckWord($word) { return in_array(strtoupper($word[0]), array('E', 'L', 'B')); } echo CheckWord('Ewok') ? 'Yes' : 'No'; // Yes echo CheckWord('word') ? 'Yes' : 'No'; // No echo CheckWord('enough') ? 'Yes' : 'No'; // Yes ?> Unless you only want to match uppercase letters. Link to comment https://forums.phpfreaks.com/topic/268502-check-that-word-begins-with-specific-letters/#findComment-1378860 Share on other sites More sharing options...
Barand Posted September 18, 2012 Share Posted September 18, 2012 Assuming 'e' is is acceptable as well as 'E' ? Link to comment https://forums.phpfreaks.com/topic/268502-check-that-word-begins-with-specific-letters/#findComment-1378861 Share on other sites More sharing options...
simonjk Posted September 18, 2012 Author Share Posted September 18, 2012 Thanks guys, that's great. What I would like to do is to return a message saying 'Location does not begin with E, B or L' if the conditions is true. How can I do this? Thanks again, Simon Link to comment https://forums.phpfreaks.com/topic/268502-check-that-word-begins-with-specific-letters/#findComment-1378907 Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 You've already been given that code. Link to comment https://forums.phpfreaks.com/topic/268502-check-that-word-begins-with-specific-letters/#findComment-1378908 Share on other sites More sharing options...
cyberRobot Posted September 18, 2012 Share Posted September 18, 2012 Hint: <?php //... echo CheckWord('Ewok') ? 'Yes' : 'No'; // Yes echo CheckWord('word') ? 'Yes' : 'No'; // No //... ?> Link to comment https://forums.phpfreaks.com/topic/268502-check-that-word-begins-with-specific-letters/#findComment-1378909 Share on other sites More sharing options...
simonjk Posted September 18, 2012 Author Share Posted September 18, 2012 Sorry guys, I perhaps should have been a little clearer with what I am trying to do. The user will enter a code word ($icao) and this code is then to check that the word begins with an E, B or L. I want to run the functions and if all is well post it to a variable. This is the code I have (thanks to you). Does it look okay? I can't get it to work. Thanks again, Simon function checkWord($icao) return in_array(strtoupper($icao[0]), array('E', 'L', 'B'), $ficao) ? TRUE : FALSE; if(checkWord() != FALSE) { //If all is well we can assign the value of POST field to a variable $userName = $_POST['userfcstarea']; } else { // if all is not well, we echo an error and exit the script echo 'Icao is invalid'; exit(); } Link to comment https://forums.phpfreaks.com/topic/268502-check-that-word-begins-with-specific-letters/#findComment-1378943 Share on other sites More sharing options...
Pikachu2000 Posted September 18, 2012 Share Posted September 18, 2012 When posting code, enclose it within the forum's . . . BBCode tags. Link to comment https://forums.phpfreaks.com/topic/268502-check-that-word-begins-with-specific-letters/#findComment-1378947 Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 You said $icao and your code says $ficao Link to comment https://forums.phpfreaks.com/topic/268502-check-that-word-begins-with-specific-letters/#findComment-1378952 Share on other sites More sharing options...
simonjk Posted September 18, 2012 Author Share Posted September 18, 2012 Code should have been... function checkWord($icao) <code> return in_array(strtoupper($icao[0]), array('E', 'L', 'B'), $icao) ? TRUE : FALSE; if(checkWord() != FALSE) { //If all is well we can assign the value of POST field to a variable $userName = $_POST['userfcstarea']; } else { // if all is not well, we echo an error and exit the script echo 'Icao is invalid'; exit(); } </code> Link to comment https://forums.phpfreaks.com/topic/268502-check-that-word-begins-with-specific-letters/#findComment-1378962 Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 You're not passing $icao to the function. Read about variable scope. Also our code tags are [ code ] without the spaces. There's also a button. Link to comment https://forums.phpfreaks.com/topic/268502-check-that-word-begins-with-specific-letters/#findComment-1378972 Share on other sites More sharing options...
Barand Posted September 18, 2012 Share Posted September 18, 2012 Quote function checkWord($icao) return in_array(strtoupper($icao[0]), array('E', 'L', 'B'), $ficao) ? TRUE : FALSE; The body of a function definition needs to be enclosed in {..}s in_array() returns true or false anyway so the ? TRUE : FALSE is redundant; The third argument to in_array() is unnecessary (and if present should be true or false) Link to comment https://forums.phpfreaks.com/topic/268502-check-that-word-begins-with-specific-letters/#findComment-1378998 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.