Regex for matching only first 4 digits in a 6 digit number

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Regex for matching only first 4 digits in a 6 digit number

Post by Live24x7 »

I am pretty new to php & SQL. Here's my requirement

My SQL db contains, amongst others, a 6 digit number in a field named 'rpin'
A user submits a pincode named ('epin')

Code: Select all

$ename=$_POST['ename']; 
$epin=$_POST['epin']; 
I want the php code to just match the first 4 digits of the user submitted code(epin) with the first four code in the existing database (rpin) and display the results

So suppose a guy enters 100018
it should retrieve all matching fields entries ranging from
100001 to 100099

How do i achieve this with regex ?

Thanks
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Regex for matching only first 4 digits in a 6 digit numb

Post by mikeashfield »

Code: Select all

<?php
    if (isset($_POST['form_value']) && preg_match('^\d{6}$', $_POST['form_value'])) {
        $search_for=substr($_POST['form_value'],1,4);
    }
?>
Use $search_for in your MySQL query. :)
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: Regex for matching only first 4 digits in a 6 digit numb

Post by Live24x7 »

@mikeashfield - thanks - great help..makes sense.. will try this out.

thnks again :o
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Regex for matching only first 4 digits in a 6 digit numb

Post by twinedev »

mikeashfield wrote:

Code: Select all

$search_for=substr($_POST['form_value'],1,4);
The above would return the 2-5 numbers, not the first 4.

Code: Select all

if (isset($_POST['epin']) && preg_match('/^(\d{4})\d{2}$/',$_POST['epin'],$regs)) {
    $strFirstFour = $regs[1];
}
Then depending on how the data is stored in the field:

If VARCHAR:

Code: Select all

$SQL = 'SELECT * FROM table WHERE rpin LIKE "'.$strFirstFour.'%" ';
If INT:

Code: Select all

$SQL = 'SELECT * FROM table WHERE rpin BETWEEN '.$strFirstFour.'01 AND '.$strFirstFour.'99';
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: Regex for matching only first 4 digits in a 6 digit numb

Post by Live24x7 »

@twinedev » thanks a lot. 8)
regex, kind of scares me.
I managed to achieve this result another way round.

Code: Select all


$emodulu =  $epin%100; // modulus operator % gives the remainder when the pin number is divided by 100
 $epin = $epin - $emodulu; // this will bring it to the nearest 100 for example 100218  becomes 100200
 $match = mysql_query("SELECT * FROM table WHERE  rpin<'$epin'+99  ); // fetches all results from 100200 to 102299

User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Regex for matching only first 4 digits in a 6 digit numb

Post by twinedev »

your statement will not get 102299 unless you change it to <= and there is nothing that limits the lower end (see the BETWEEN example I gave)

-Greg
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: Regex for matching only first 4 digits in a 6 digit numb

Post by Live24x7 »

twinedev wrote:your statement will not get 102299 unless you change it to <= and there is nothing that limits the lower end (see the BETWEEN example I gave)

-Greg

Very true: I need to add these two.

How can i miss such basic things. It would have been a disaster, had i left the lower end unplugged. :(

Saying Thanks is just not enough !!
Post Reply