REGEX: PHP kodu kullanarak, JavaScripts yorumlarına nasıl kaldırılır

0 Cevap php

I am combining all my javascriupt into one neat file in order to lower http requests! Im stuck removing the comments /* comments */ and // comments. My level is by far below minification or parsing stuff. I know how to make macaroni strings. Anything more complex than that, you will not find in my computer or kitchen, SO:

QUESTION

meanwhile at combining it to one file, i want to remove all comments.
What is the correct regex for this?

<?php
header('Content-type: text/javascript');    
$offset = 60 * 60 * 24; // Cache for a day
header ('Cache-Control: max-age=' . $offset . ', must-revalidate');
header ('Expires: ' . gmdate ("D, d M Y H:i:s", time() + $offset) . ' GMT');

ob_start("compress");
function compress($buffer) {

# NOT SURE, not all new lines are removed??
# remove tabs, spaces, newlines, etc.
$buffer = str_replace(array("\r\n", "\r", "\t", '  ', '    '), '', $buffer);  

# WORKS !!!
# remove comments / XXXXXXX
$buffer = preg_replace('(// .+)', '', $buffer);

######################################################################## 
# !! STUCK HERE !! OUTPUT FILE LOOKS OK BUT WEBSITE DOESNT LOAD OK IF THIS IS ON
# remove comments / * XXX  (enters etc) XXXX  * /
# $buffer = preg_replace('#/\*(?:.(?!/)|[^\*](?=/)|(?<!\*)/)*\*/#s', '', $buffer);
########################################################################        

return $buffer;
}

include('../file1.js');
include('../file2.js');  
ob_end_flush();
?>

Bu yakalamak ve aşağıdaki silin olsaydı harika olurdu:

/* XXXX */

/* 
  XXXX
  XXXX
*/

Thats all! Cant bile ben doğru maç için BULUNAMADI bu inanılmaz araç, ile nomatter i kullanmak ne regex işe almak:

RegExp: /\/\*(?:.(?!/)|[^\*](?=/)|(?<!\*)/)*\*\//gs
pattern: \/\*(?:.(?!/)|[^\*](?=/)|(?<!\*)/)*\*\/
flags: gs

http://gskinner.com/RegExr/

0 Cevap