Page 1 of 1

problem with eregi function in PHP

Posted: Thu Jul 23, 2009 5:00 am
by harshilshah
here's my code

Note: I am using php 5.0.3

Code: Select all

class ArchiveExtractor {
 
 
  /**
 
    Extract Archive(TarGzip, Zip) based on file suffix
 
    @param string Archive file
    @param string extraction path
 
    @return array
 
  */
  function extractArchive($archFile, $extractPath=".") {
    echo "ARCH: ".$archFile."<br />ExtPath: $extractPath";
    $result="";
    
    if(eregi("tar|gz|tar.gz|tgz",$archFile)) {
      $result=$this->extractTarGzip($archFile,$extractPath);
    }
    else if(eregi("zip",$archFile)) {
      $result=$this->extractZip($archFile,$extractPath);
    }
    
    /* Return result */
    return $result;
  }
 
  /**
 
    TarGzip file extractor function
    
    @param string Archive file
    @param string extraction path
    
    @return string
    
  */
  function extractTarGzip($archFile, $extractPath=".") {
    /* include TAR library */
    require_once 'pcltar.func.php';
    
    /* extract and return list of extracted files */
    return PclTarExtract($archFile,$extractPath);
  }
  
  
  /**
 
    Zip file extractor function
 
    @param string Archive file
    @param string extraction path
 
    @return array
 
  */
  function extractZip($archFile, $extractPath=".") {
    /* include Zip Library file */
    require 'pclzip.class.php';
    
    
    /* Extract */
    $zip=new PclZip($archFile);
    
    /* list of extracted files */
      return $zip->extract($extractPath);
  }
  
  /**
    Get Tar/Gzip archive's file list
    @param string
    @return array
  */
  function getTarGzipList($archFile) {
    /* include TAR library */
    require_once 'pcltar.func.php';
    
    /* return list */
    return PclTarList($archFile);
  }
  
  /**
    Get Zip archive's file list
    @param string
    @return array
  */
  function getZipList($archFile) {
    /* include Zip Library file */
    require 'pclzip.class.php';
 
 
    /* Extract */
    $zip=new PclZip($archFile);
 
    /* return */
    return $zip->listContent();
  }
  
  
}
?>
 
here's my error





Deprecated: Function eregi() is deprecated in C:\wamp\www\godnels\imagealbum\ArchiveExtractor.class.php on line 42

Deprecated: Function eregi() is deprecated in C:\wamp\www\godnels\imagealbum\ArchiveExtractor.class.php on line 45
album submitted

Re: problem with eregi function in PHP

Posted: Thu Jul 23, 2009 6:49 am
by JAB Creations
Don't use eregi as it's been deprecated and won't be available in PHP6. I'm pretty sure you'll want to use preg_match.

Also your script doesn't look overly indepth though as a good practice when you do things like form verification you'll want to execute regular expressions as the last step of verification as it generates the most server load. So if you can catch a validation error (such as length) first then you'll be spared from creating unnecessary load (and thus poorer server performance). :wink:

Re: problem with eregi function in PHP

Posted: Thu Jul 23, 2009 6:59 am
by harshilshah
JAB Creations wrote:Don't use eregi as it's been deprecated and won't be available in PHP6. I'm pretty sure you'll want to use preg_match.

Also your script doesn't look overly indepth though as a good practice when you do things like form verification you'll want to execute regular expressions as the last step of verification as it generates the most server load. So if you can catch a validation error (such as length) first then you'll be spared from creating unnecessary load (and thus poorer server performance). :wink:
how do i use it in my case. i dont know how to use it.

Re: problem with eregi function in PHP

Posted: Thu Jul 23, 2009 8:21 am
by Dynamis
preg_match

That should answer all of your questions (there are examples if you scroll down) but if not let me know and I could give you another example.

Re: problem with eregi function in PHP

Posted: Wed Jul 29, 2009 3:17 am
by harshilshah
here's the code i used with preg_match function

Code: Select all

 function extractArchive($archFile, $extractPath=".") {
    echo "ARCH: ".$archFile."<br />ExtPath: $extractPath";
    $result="";
    
    if(preg_match("%tar%gz%tar.gz%tgz",$archFile)) {
      $result=$this->extractTarGzip($archFile,$extractPath);
    }
    else if(preg_match("%zip%",$archFile)) {
      $result=$this->extractZip($archFile,$extractPath);
    }
    
    /* Return result */
    return $result;
  }
here's the error i am getting. Please help me its urgent.
Warning: preg_match() [function.preg-match]: Unknown modifier 'g' in C:\wamp\www\godnels\imagealbum\ArchiveExtractor.class.php in line5

Re: problem with eregi function in PHP

Posted: Wed Jul 29, 2009 9:14 am
by McInfo
In this case, it might be more appropriate to use pathinfo() and in_array() rather than regular expression functions.

Edit: This post was recovered from search engine cache.

Re: problem with eregi function in PHP

Posted: Wed Jul 29, 2009 5:18 pm
by pickle
If you notice in all the examples, the patterns all start and end with the same character. That character is called the "delimiter". It looks like you've chosen "%" as your delimiter. Traditionally "/" is used, but "%" will work just as well.

Your first pattern is filled with multiple occurrences of "%". If you want them to be in your pattern as literal characters (ie, you want to match a percent sign & not have it treated as a delmiiter) then you need to escape it like this: "\%". Your error is being thrown because the regex engine sees the second occurrence of "%" as the closing delimiter, and tries to interpret the rest of your pattern as pattern modifiers, of which "g" is not valid.

If you're going to use regular expressions, I highly recommend reading through the Regex Crash Course Tutorial we've got.