O
O
onogur2014-03-12 22:13:43
PHP
onogur, 2014-03-12 22:13:43

Why does RegExp put Apache2?

To remove multiline comments like /*...*/ I wrote the following regular expression: I
/\/\*(.|.*\n){0,}/
tested it on a small example, everything works. But as soon as a large file larger than 300 kb is supplied as input, the server crashes. I can't decide what the problem is. The regular for finding and deleting single-line comments works properly. Here are the regular expressions for single-line comments:
/\/{2}[a-zA-Z].*/
/\/{2}\s.*/
Both Denver and Apache fit Ubuntu. What's my mistake?
Here is the full script code:

<?php
function slib_compress_script( $buffer ) {
$buffer = preg_replace("/\/{2}\s.*/", "", $buffer);//однострочные
$buffer = preg_replace("/\/{2}[a-zA-Z].*/", "", $buffer);//однострочные
//$buffer = preg_replace("/\/\*(.|.*\n){0,}/", "", $buffer);//Та самая регулярка на многострочные комментарии
return trim( $buffer );
}
$jsLibsPatch = "libs/";
$jsLibsData = '';
$pattern = '/\/\*[\s\S]*?\*\//';

$folder = scandir($jsLibsPatch); 
foreach($folder as $file) 
{
if(($file !=".") && ($file !="..")){
  $jsFileName = $jsLibsPatch.$file;
  $fileHandle = fopen($jsFileName, 'r');
  $jsLibsDataNew = fread($fileHandle, filesize($jsFileName));
  $jsLibsDataNew = slib_compress_script($jsLibsDataNew);
  $jsLibsData .= "\n" . $jsLibsDataNew;
  fclose($fileHandle);
}}
  
$jsPatch = "file/";
$jsData = '';
$folder = scandir($jsPatch);   
foreach($folder as $file) 
{
if(($file !=".") && ($file !="..")){
  $jsFileName = $jsPatch.$file;
  $fileHandle = fopen($jsFileName, 'r');
  $jsDataNew = fread($fileHandle, filesize($jsFileName));
  $jsDataNew = slib_compress_script($jsDataNew);
  $jsData .= "\n" . $jsDataNew;
  fclose($fileHandle);
}}

header("Content-Type: text/javascript");
if (isset($jsData)) {
//$jsLibsData = preg_replace('/\s+/', ' ', $jsLibsData);
//$jsData = preg_replace('/\s+/', ' ', $jsData);
$newJS = $jsLibsData."\n".$jsData;
echo $newJS;
} else {
echo "// Files not avalable or no files specified.";
}

 ?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AxisPod, 2014-03-13
@AxisPod

Things like this are easier to do without regular expressions. And the regex seems to consume a lot of memory.

V
Vampiro, 2014-03-13
@Vampiro

Merge several files into one and remove comments to reduce the size of the final file.

Let me google for you.
stackoverflow.com/questions/503871/best-way-to-aut...
Although in fact this is a waste of time.
Threat regex for comments is far from trivial)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question