Results 1 to 3 of 3

Thread: IP address regex problem with preg_match_all

  1. #1
    Join Date
    Aug 2010
    Posts
    20

    IP address regex problem with preg_match_all

    I made small script to parse text files for IP addresses and email addresses using preg_match_all intrusions. after execution program I get a result as IP address with missing one last digit. I can’t figure out this coding problem.

    Please give me any suggestions on program code
    Code:
     <?php
     function getdirfiles($dir, $no_dots=false)
     {
     $files = array();
     $dh = @opendir($dir);
     if ($dh!=false)
     {
     while (false !== ($filename = readdir($dh)))
     {
     $files[] = $filename;
     }
    
     if ($no_dots)
     {
     while(($ix = array_search('.',$files)) > -0) 
     unset($files[$ix]);
    
     while(($ix = array_search('..',$files)) > -0)
     unset($files[$ix]);
     }
    
     sort($files);
    
     }
    
     return $files;
    
     }
    
     $ipreg="/([1-9]{1}\d{1}|[1-9]{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1}|[1-9]{1}\d{1}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1}|[1-9]{1}\d{1}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1}|[1-9]{1}\d{1}|1\d\d|2[0-4]\d|25[0-5])/";
     $emailreg="/(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}/";
    
     $folder = '/home/eger/udpchan/admin/test';
    
     $files = getdirfiles($folder, true);
    
    
     // Show IPs found
     foreach ($files as $file) {
     $contents = file_get_contents($folder."/".$file);
     preg_match_all($ipreg, $contents, $ips);
     $ips = array_unique($ips[0]);
     print_r($ips);
     }
    
     // Show email address found
     foreach ($files as $file) {
     $contents = file_get_contents($folder."/".$file);
     preg_match_all($emailreg, $contents, $emails);
     $emails = array_unique($emails[0]);
     print_r($emails);
     }
    
     //Show Files
     print_r(scandir(test,true)); 
     ?>

  2. #2
    Join Date
    Aug 2010
    Posts
    29

    Re: IP address regex problem with preg_match_all

    Make your preg_match_all into a 3-step operation

    1. Check for last quadrant of IP address contains exactly 3 valid digits
    2. Check for last quadrant of IP address contains exactly 2 valid digits
    3. Check for last quadrant of IP address contains exactly 1 valid digits
    This will take care of your last digit

  3. #3
    Join Date
    Jul 2010
    Posts
    38

    Re: IP address regex problem with preg_match_all

    Try this PHP code:

    Code:
    /* Here we set the variable 'q' which represents a single quadrant of a valid IP address. */
     $q="(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])";
     $ipreg="/$q\.$q\.$q\.(1\d\d|2[0-4]\d|25[0-5])|$q\.$q\.$q\.([1-9]\d)|$q\.$q\.$q\.(\d)/";

Similar Threads

  1. Problem in matching Regex
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 04:12 AM
  2. Regex to verify email address
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 17-02-2010, 03:50 AM
  3. Problem in handling exception with Regex
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 01:56 AM
  4. Regex to find email address
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 01:55 AM
  5. Regex problem in construction
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 04-02-2010, 03:59 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,399,404.88312 seconds with 17 queries