1

I have next code in php

<?
$stop_ips_masks = array(
        "66\.249\.[6-9][0-9]\.[0-9]+", 
        "74\.125\.[0-9]+\.[0-9]+", 
    );
foreach ( $stop_ips_masks as $k=>$v )
{
    if ( preg_match( '#^'.$v.'$#', $_SERVER['REMOTE_ADDR']) )
        echo found;
}
?>

and it works fine. But I would like to load this array from txt file, so I wrote code

<?
$stop_ips_masks = file('array.txt');
foreach ( $stop_ips_masks as $k=>$v )
{
    if ( preg_match( '#^'.$v.'$#', $_SERVER['REMOTE_ADDR']) )
        echo found;
}
?>

where array.txt is

"66\.249\.[6-9][0-9]\.[0-9]+",
"74\.125\.[0-9]+\.[0-9]+",

and this code doesn't work. Please help me what is wrong with my code?

3
  • Will your strings have any newlines or other special characters embedded in them? Commented Jul 27, 2013 at 8:00
  • Yes, I will add later more lines the same type like in example. Commented Jul 27, 2013 at 9:36
  • Is your file in the same directory as the one you run the script from? Try using absolute path. Also, the commas at the end of each line and the quotes may spoil your script. And remember - check the error log ;) Commented Jul 27, 2013 at 17:09

4 Answers 4

1

you need to convert your text into array first, because what your doing right now includes a , in your regexp pattern

$stop_ips_masks = file_get_contents('array.txt');
$stop_ips_masks = explode("," $stop_ips_masks);

You might also need to do a trim operation and remove the newlines as well. it would be easier if you stick to one record separator. like comma or newline. not both.

If you remove the , from your text file then you can do

$stop_ips_masks = file('array.txt');
Sign up to request clarification or add additional context in comments.

1 Comment

Also doesn't work, I think that problem maybe is in slashes but don't know how to solve it.
0

Try something like;

<?
$file = file('array.txt');
$stop_ips_masks = explode(",",$file);
foreach ( $stop_ips_masks as $k=>$v )
{
    if ( preg_match( '#^'.$v.'$#', $_SERVER['REMOTE_ADDR']) )
        echo found;
}
?>

Comments

0

I believe your issue is the quotes and tailing comma. While needed to make a array in php they are invalid when sourced from the file.

    $stop_ips_masks = file('array.txt', FILE_IGNORE_NEWLINES);
    foreach ( $stop_ips_masks as $k=>$v )
    {
          $v = trim(rtrim($v, ","), '"');
          if ( preg_match( '#^'.$v.'$#', $_SERVER['REMOTE_ADDR']) )
          echo found;
    }

Comments

0

Another option is to store your strings "raw" in the file as lines:

66\.249\.[6-9][0-9]\.[0-9]+
74\.125\.[0-9]+\.[0-9]+

Then read the file, putting each line into the array:

$array = file('array.txt');

1 Comment

Or just $array = file('array.txt') as it's the same ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.