Jump to content

preg_grep outupt to a string rather than an array?


awreneau

Recommended Posts

Below I have a piece of code from a page I'm building.  The page is a self processing form that accepts user input (ip address) and validates the IP.  If the IP checks out it preg_grep checks for the IP in the file /var/www/test/mail.log.  If the ip is present in the log it is printed out with the following example:

 

array

{

data goes here

}

 

I've introduced the in_array function to see if the ip address is in the array $matches.  I'd like the statement if (in_array("/$grep_ip/iU"..... to check if the ip address is in the file.  If true display the results, if not indicate with an error on the screen produced by the else.

 

The problem is that I always get the error produced from the else statement "The ip you entered has not contacted the Mail Relay as ofAugust 7, 2008, 6:23 pm "

 

Can someone tell me what I've done wrong or make a suggestion as to a better way?

 

 

if ( preg_match("{^\b((25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)\.){3}(25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)\b$}", $grep_ip))
               {
                 $matches = preg_grep( "/$grep_ip/iU", file("/var/www/test/mail.log"));
                    if ( in_array("/$grep_ip/iU", $matches, $string))

                    {

                         echo '<pre>';
                         print_r($string);
                         $grep_ip = "";
                    }
                    else
                    {
                         echo "The ip you entered has not contacted the Mail Relay as of"; echo date("F j, Y, g:i a");
                    }
               }
               else
               {
                    echo "The IP address is invalid";

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.