M
M
modesden2016-06-07 08:08:28
PHP
modesden, 2016-06-07 08:08:28

Why Segmentation error (memory dumped) when executing preg_match_all function?

$array = scandir('damps');
        $f = fopen('dampFirm.sql', 'w');
        foreach($array as $file){
            if($file != '.' && $file != '..'){
                $content = file_get_contents('damps/' . $file);
                /*var_dump($content);
                die;*/
                $result = preg_match_all("/INSERT INTO(.|\n|\r)*\)\;/U",  $content, $inserts);
                var_dump($inserts[0]);
                die;
                if($result) {
                    foreach ($inserts[0] as $insert) {
                        $str = str_replace('` (`id`, ', '` (', $insert);
                        $str = preg_replace("/\(\d+, /", '(', $str) . "\n";
                        fwrite($f, $str);
                    }
                }
            }
        }
Here is the code.
var_dump($content); Gives out and does not go further. Throws Segmentation Fault (memory dumped). $content contains ~100,000 lines. Please tell me what could be the problem. Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Melkij, 2016-06-07
@modesden

(.|\n|\r)*

A classic misuse of regular expressions. On a sufficient string size, the recursion stack is expected to survive and fall into a segfault.
Nafig you here at all memorization surrendered? I don't see you using it further down the code. I'll rewrite a bit:
/INSERT INTO.+?\)\;/s

V
Vladislav Startsev, 2016-06-07
@esvlad

die alias exit
exit - displays a message and terminates the current script

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question