eldan88 Posted October 6, 2013 Share Posted October 6, 2013 Hey guys. I am trying to create an expression using preg match.(Kinda new to it) When I create the expression I want it to match the full number. Not each individual number. For example below.. the following expression below I have [20-30] but it also marches the number 2 aswell. How do i have it match the full number 20-30??? <?php if (preg_match("#[20-30]#", "2")) { echo "Match was found"; } else { echo "Match was not found"; } Quote Link to comment https://forums.phpfreaks.com/topic/282764-need-help-with-expressions/ Share on other sites More sharing options...
vinny42 Posted October 6, 2013 Share Posted October 6, 2013 [20-30] matches any character 0,2,3 and -. (I trust that you googles a few tutorials about how regular expressions work? That saves you a lot of these kinds of "beginners" problems. :-) "20-30" is not a number, it's a string, and it is much more efficient toe use strpos to find that in a variable. Quote Link to comment https://forums.phpfreaks.com/topic/282764-need-help-with-expressions/#findComment-1452841 Share on other sites More sharing options...
Psycho Posted October 6, 2013 Share Posted October 6, 2013 You need to crawl before you walk. What you have doesn't appear to be anything like what you are after. The square brackets are defining a class of characters to be matched - not specific sequences of characters. What, EXACTLY, are you trying to achieve? Show some potential input strings and the matches you are trying to achieve. If you are trying to match the number VALUE that is from 20 to 30, then you should be looking for two characters where the first character is a '2' and the second characters is a numeral OR where both characters are '30' preg_match('#2\d|30#', $input) Quote Link to comment https://forums.phpfreaks.com/topic/282764-need-help-with-expressions/#findComment-1452843 Share on other sites More sharing options...
eldan88 Posted October 6, 2013 Author Share Posted October 6, 2013 Psycho. I see what you are saying. I actually did some more research and think is would be a lot simpler if I used the PHP range function. Quote Link to comment https://forums.phpfreaks.com/topic/282764-need-help-with-expressions/#findComment-1452844 Share on other sites More sharing options...
eldan88 Posted October 6, 2013 Author Share Posted October 6, 2013 (edited) You need to crawl before you walk. What you have doesn't appear to be anything like what you are after. The square brackets are defining a class of characters to be matched - not specific sequences of characters. What, EXACTLY, are you trying to achieve? Show some potential input strings and the matches you are trying to achieve. If you are trying to match the number VALUE that is from 20 to 30, then you should be looking for two characters where the first character is a '2' and the second characters is a numeral OR where both characters are '30' preg_match('#2\d|30#', $input) What I am trying to create is a delivery address validator. I want enter address's that are outside a delivery range of a restaurant Lets say they doesn't delivery to 50th street. What I want to do is add all the building numbers on 50th street. Lets so for this examples the building number ranges from 20-30 on 50th street. If someone types in "25 50th st" on the checkout page, it will restrict them from ordering.(The characters will be ignored. It will just validate the numbers.) However the challenge that I am facing is how do I validate the street along the building numbers? I am going to be creating an array for building numbers and street that the restaurants won't delivery to. Below is the code I have now for the building ranges <?php $range = range(20, 30); $input_number = 30; $street_number = 50; if(in_array($input_number, $range)){ echo "The address is within the delivery range"; } else { echo "Address is not within the range"; } ?> Edited October 6, 2013 by eldan88 Quote Link to comment https://forums.phpfreaks.com/topic/282764-need-help-with-expressions/#findComment-1452847 Share on other sites More sharing options...
.josh Posted October 7, 2013 Share Posted October 7, 2013 [20-30] matches any character 0,2,3 and -. (I trust that you googles a few tutorials about how regular expressions work? That saves you a lot of these kinds of "beginners" problems. :-) "20-30" is not a number, it's a string, and it is much more efficient toe use strpos to find that in a variable. Actually character classes support ranges with hyphens, so that would actually match 2,0,1,2(again),3, or 0 (again). The "problem" is that it only supports single characters for ranges (unless escape sequences are involved, but you can't use them for this). Psycho showed correct way to do it with regex. The \d is shorthand for [0-9], so another way of writing it would be #2[0-9]|30# Quote Link to comment https://forums.phpfreaks.com/topic/282764-need-help-with-expressions/#findComment-1452875 Share on other sites More sharing options...
vinny42 Posted October 7, 2013 Share Posted October 7, 2013 Actually character classes support ranges with hyphens, I knew that, I dind't know that 0-3 is a valid range. :-) Quote Link to comment https://forums.phpfreaks.com/topic/282764-need-help-with-expressions/#findComment-1452901 Share on other sites More sharing options...
eldan88 Posted October 7, 2013 Author Share Posted October 7, 2013 Got it. Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/282764-need-help-with-expressions/#findComment-1452922 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.