PHP: check user input against text file?

I have the following street names and house numbers in a text file:

Albert Dr: 4116-4230, 4510, 4513-4516
Bergundy Pl: 1300, 1340-1450
David Ln: 3400, 4918, 4928, 4825
Garfield Av: 5000, 5002, 5004, 5006, 8619-8627, 9104-9113

This data represents the boundary data for a local neighborhood (i.e., what houses are inside the community).

I want to make a PHP script that will take a user's input (in the form of something like "4918 David Lane" or "3000 Bergundy") search this list, and return a yes/no response whether that house exists within the boundaries.

What would be an efficient way to parse the input (regex?) and compare it to the text list?

Thanks for the help!


It's better to store this info in a database so that you don't have to parse out the data from a text file. Regexes are also not generally applicable to find a number in a range so a general purpose language is advised as well.

But... if you want to do it with regexes (and see why it's not a good idea)

To lookup the numbers for a street use

David Ln:(.*)

To then get the numbers use


You could simply import the file into a string. After this is done, breack each line of the file in an array so Array(Line 1=> array(), Line 2=> array(), etc. After this is done, you can explode using :. After, you'll simply need to search in the array. Not the fastest way, but it may be faster then regex.

You should sincerely consider using a database or re-think how your file are.

Try something like this, put your street names inside test.txt.. Now that you are able to get the details inside the text file, just compare it with the values that you submit in your form.

 $filename = 'test.txt';
        if(file_exists($filename)) {
            if($handle = fopen($filename, 'r')) {
                $name = array();
                while(($file = fgets($handle)) !==FALSE) {
                    preg_match('#(.*):(.*)#', $file, $match);
                    $array = explode(',', $match[2]);
                    foreach($array as $val) {
                        $name[$match[1]][] = $val;


As mentioned, using a database to store street numbers that are relational to your street names would be ideal. I think a way you could implement this with your text file though is to create a a 2D array; storing the street names in the first array and the valid street numbers in their respective arrays.

Parse the file line by line in a loop. Parse the street name and store in array, then use a nested loop to parse all of the numbers (for ones in a range like 1414-1420, you can use an additional loop to get each number in the range) and build the next array in the initial street name array element. When you have your 2D array, you can do a simple nested loop to check it for a match.

I will try to make a little pseudo-code for you..


$addresses = array();
$counter = 0;
$line = file->readline
  $addresses[$counter] = parse_street_name($line);
  $numbers_array = parse_street_numbers($line);
  foreach($numbers_array as $num)
    $addresses[$counter][] = $num;

  $line = file->readline

It's better if you store your streets in a separate table with IDs, and store numbers in separate table one row for each range or number and street id.

For example:


ID, street
1, Albert Dr
2, Bergundy Pl
3, David Ln
4, Garfield Av


street_id, house_min, house_max
1, 4116, 4230
1, 4510, 4510
1, 4513, 4516
2, 1300, 1300
2, 1340, 1450

In the rows, where no range but one house number, you set both min and max to the same value.

You can write a script, that will parse your txt file and save all data to db. That should be as easy as several loops and explode() with different parameters and some insert queries too.

Then with first query you get street id

SELECT id FROM streets WHERE street LIKE '%[street name]%'

After that you run second query and get answer, is there such house number on that street

FROM houses 
WHERE street_id = [street_id] 
    AND [house_num] BETWEEN house_min AND house_max

Inside [...] you put real values, dont forget to escape them to prevent sql injections...

Or you even can run just one query using JOIN.

Also you should make sure that your given house number is integer, not float.

Need Your Help

SQL Server Bulk Insert Issue on Windows7

sql-server security windows-7 bulkinsert sql-server-2005-express

I’ve got an application that uses SQL Server Express 2005 SP3. This application is distributed to several hundred users, each of whom is running XP. However, our company will be moving to Windows...

Retrieved Pinterest Access Token v Token generator Tool


what is the difference between the access token that can be retrieved through the auth process and the one that is generated here: