james909 Posted August 21, 2014 Share Posted August 21, 2014 How to a check if a php variable value is one of the following characters: A to J, or ,k to t, (ie.: A B C D E F G H I J k l m n o p q r s t) I tried: if (preg_match[A-Jk-t], $checkChar) { // do this } But it did not work Quote Link to comment https://forums.phpfreaks.com/topic/290578-preg_match-help/ Share on other sites More sharing options...
Jacques1 Posted August 21, 2014 Share Posted August 21, 2014 Whatever this is supposed to be, it's not PHP code. Where are the parentheses for the function call? Where are the quotes for the string? Where are the delimiters for the regex? Quote Link to comment https://forums.phpfreaks.com/topic/290578-preg_match-help/#findComment-1488558 Share on other sites More sharing options...
cyberRobot Posted August 21, 2014 Share Posted August 21, 2014 Instead of using a regular expression, you could just create an array of values (A-J and k-t). You can then see if $checkChar is in the array with in_array(). More information about in_array() can be found here: http://php.net/manual/en/function.in-array.php Also note that you could create the array using a combination of range() and array_merge(). Quote Link to comment https://forums.phpfreaks.com/topic/290578-preg_match-help/#findComment-1488564 Share on other sites More sharing options...
Jacques1 Posted August 21, 2014 Share Posted August 21, 2014 What's the benefit? It's literally twice as much code and arguably less readable. Quote Link to comment https://forums.phpfreaks.com/topic/290578-preg_match-help/#findComment-1488565 Share on other sites More sharing options...
cyberRobot Posted August 21, 2014 Share Posted August 21, 2014 What's the benefit? It's literally twice as much code and arguably less readable. I guess it depends on what the code is used for. When I've had code like this in the past, the array of characters is used in other parts of the script. However, you're probably right about regular expressions fitting better in this case. Quote Link to comment https://forums.phpfreaks.com/topic/290578-preg_match-help/#findComment-1488568 Share on other sites More sharing options...
cyberRobot Posted August 21, 2014 Share Posted August 21, 2014 @james909 - If you're looking for guidance on using preg_match(), the PHP manual has some examples that might help: http://php.net/manual/en/function.preg-match.php Quote Link to comment https://forums.phpfreaks.com/topic/290578-preg_match-help/#findComment-1488571 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.