awreneau Posted August 7, 2008 Share Posted August 7, 2008 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"; Quote Link to comment https://forums.phpfreaks.com/topic/118701-preg_grep-outupt-to-a-string-rather-than-an-array/ Share on other sites More sharing options...
effigy Posted August 8, 2008 Share Posted August 8, 2008 Try: $matches = preg_grep('/' . preg_quote($grep_ip) . '/iU', file('/var/www/test/mail.log')); You shouldn't need in_array. Quote Link to comment https://forums.phpfreaks.com/topic/118701-preg_grep-outupt-to-a-string-rather-than-an-array/#findComment-611599 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.