Jump to content

Need help with Expressions


eldan88

Recommended Posts

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";
}

Link to comment
Share on other sites

[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.

Link to comment
Share on other sites

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)
Link to comment
Share on other sites

 

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 by eldan88
Link to comment
Share on other sites

[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#

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.