kartik-v / mpdf

Fork of the mPDF latest DEV library (unofficial) with composer and packagist support.
GNU General Public License v2.0
40 stars 55 forks source link

preg_replace(): Compilation failed: regular expression is too large at offset ... #4

Open CriStaS opened 9 years ago

CriStaS commented 9 years ago

There is an error in cssmgr.php:164 :

    if (preg_match('/@media/',$CSSstr)) { 
        preg_match_all('/@media(.*?)\{(([^\{\}]*\{[^\{\}]*\})+)\s*\}/is',$CSSstr,$m);
        for($i=0; $i<count($m[0]); $i++) {
            if ($this->mpdf->CSSselectMedia && !preg_match('/('.trim($this->mpdf->CSSselectMedia).'|all)/i',$m[1][$i])) { 
                $CSSstr = preg_replace('/'.preg_quote($m[0][$i],'/').'/','',$CSSstr);
            }
            else {
                $CSSstr = preg_replace('/'.preg_quote($m[0][$i],'/').'/',' '.$m[2][$i].' ',$CSSstr);
            }
        }
    }

On large CSS files with complicated rules it may cause regexp limit when replacing by preg_replace:

exception 'yii\base\ErrorException' with message 'preg_replace(): Compilation failed: regular expression is too large at offset 70187' in .../vendor/kartik-v/mpdf/classes/cssmgr.php:164

... but (as far as I can see) there is no real need in preg_replace() in this place. We have exact text, matched by preg_match_all() and now we can use str_replace() (or mb_str_replace()) to clear it out.

P.S. I don't offer pull request because I can miss something

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/26449451-preg_replace-compilation-failed-regular-expression-is-too-large-at-offset?utm_campaign=plugin&utm_content=tracker%2F7827090&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F7827090&utm_medium=issues&utm_source=github).
kartik-v commented 9 years ago

Thanks... you may refer it to the mpdf author... to fix it in source... and I can extract it into this repo OR submit a PR if you can.