ungzip file

Функция для извлечения загзипованного файла без чтения в память. Антоним функции gzip_file.

// Extracts gzipped file without reading it into memory.
//
// $source - full path of src file
// $dest - [optional, default ''] full path of destination file.
//         If empty, dest = sourse - '.gz'.
//         If $dest == $source, function returns FALSE.
//
// Returns size of gzipped file or FALSE on error.
public static function ungzip_file($source, $dest='')
{
    if ($dest == '')
        $dest = preg_replace('/.gz$/i', '', $source);

    $error = false;

    if($fp_in = gzopen($source, 'rb'))
    {
        if($fp_out = fopen($dest, 'wb'))
        {
            while(!feof($fp_in))
                fwrite($fp_out, gzread($fp_in,1024*512));
            gzclose($fp_in);
        }
        else
            $error = true;

        fclose($fp_out);
    }
    else
        $error = true;

    if($error)
        return false;
    else
        return filesize($dest);
}

preg_match

Если второй день не удается понять, почему не работает 100%-рабочее регулярное выражение, надо посмотреть ошибку с помощью этой функции:

function pcre_error_decode() 
{
    switch (preg_last_error()) 
    {
        case PREG_NO_ERROR : return 'No error'; 
        case PREG_INTERNAL_ERROR : return 'An internal PCRE error'; 
        case PREG_BACKTRACK_LIMIT_ERROR : return 'Backtrack limit exceeded'; 
        case PREG_RECURSION_LIMIT_ERROR : return 'Recursion limit exceeded'; 
        case PREG_BAD_UTF8_ERROR : return 'Bad UTF-8 error'; 
        case PREG_BAD_UTF8_OFFSET_ERROR: return 'Bad UTF-8 offset error'; 
        default: return 'unknown error';
    }
}

Модификатор m в регулярках php

Сколько пользуюсь регулярками в php, а уяснил только сейчас, что модификатор m работает только для символа \n. А если в конце строки \r\n (windows'овский формат), то \r надо учитывать отдельно.

А что это за модификатор, кстати? А вот:

m (PCRE_MULTILINE)

By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless D modifier is set). This is the same as Perl. When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.