Page 1 of 1
Regex for matching only first 4 digits in a 6 digit number
Posted: Mon Nov 21, 2011 6:04 am
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
Re: Regex for matching only first 4 digits in a 6 digit numb
Posted: Mon Nov 21, 2011 6:46 am
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.

Re: Regex for matching only first 4 digits in a 6 digit numb
Posted: Mon Nov 21, 2011 7:40 am
by Live24x7
@mikeashfield - thanks - great help..makes sense.. will try this out.
thnks again

Re: Regex for matching only first 4 digits in a 6 digit numb
Posted: Mon Nov 21, 2011 10:07 am
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';
Re: Regex for matching only first 4 digits in a 6 digit numb
Posted: Mon Nov 21, 2011 12:14 pm
by Live24x7
@twinedev ยป thanks a lot.
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
Re: Regex for matching only first 4 digits in a 6 digit numb
Posted: Mon Nov 21, 2011 12:52 pm
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
Re: Regex for matching only first 4 digits in a 6 digit numb
Posted: Mon Nov 21, 2011 1:15 pm
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 !!