Cum sa elimini comentariile din codul PHP?
2Exista diverse situatii cand esti nevoit sa simplifici codul PHP foarte mult, aducandu-l la o forma care poate fi interpretata de aplicatia ta. Recent m-am izbit de o problema foarte interesanta privind simplificarea codului – stergerea tuturor comentariilor in mod automat dintr-un cod PHP. Avand in vedere ca imi place sa caut provocari noi, am decis sa rezolv problema prin doua variante – prin metoda usoara care e aproape rezolvata automat de PHP sau prin metoda complicata ce presupune interpretarea efectiva a codului. Acest articol va prezenta atat metoda simpla pentru cei mai lenesi dintre voi cat si rezolvarea „hardcode”, ce presupune si cunostinte mai avansate.
Cea mai simpla metoda de a sterge comentariile din cod PHP
Metoda simpla presupune folosirea functiei token_get_all() din PHP ce va parsa tot codul PHP trasnformandu-l in token-uri ce fac parte din lexicul motorului Zend.
<?php function removeComments($string) { $newStr = ''; $commentTokens = array(T_COMMENT); if (defined('T_DOC_COMMENT')) $commentTokens[] = T_DOC_COMMENT; // PHP 5 if (defined('T_ML_COMMENT')) $commentTokens[] = T_ML_COMMENT; // PHP 4 $tokens = token_get_all($string);//extragem toate tokenurile foreach ($tokens as $token) { //parcurgem fiecare token if (is_array($token)) { //daca e comentariu if (in_array($token[0], $commentTokens)) continue; //trecem peste $token = $token[1]; } $newStr .= $token; //adaugam orice token care nu e comentariu } return $newStr; } ?>
Sterge comentariile in PHP folosind metoda low level
Asa cum am precizat initial, aceasta functie necesita cunostinte avansate de programare in PHP pentru a fi intelesa. Pe scurt, functia de mai jos incearca sa elimine comentariile din PHP (mai putin cele ce incep cu #, acesta e un todo simplu de rezolvat) folosindu-se efectiv de parsarea codului. Incercam sa ne asiguram ca nu avem ghilimele simple si duble. Cand acest criteriu este indeplinit cautam comentarii, in caz contrar ignoram tot pana cand ghilimele sunt inchise. Trebuie sa mentionez ca functia se comporta corect doar cand codul parsat este bun din punctul de vedere al lexicului PHP. Desi m-am straduit sa fac functia sa reactioneze frumos si in restul situatiilor, nu garantez stabilitatea rezultatului.
<?php function removeComments($content) { $quotes = false; $comments = false; $len = strlen($content); $pos = 0; while($pos < $len) { //search for closing quotes if open exists while($quotes !== false && $pos < $len && ($p = stripos($content, $quotes, $pos)) !== false) { if($p == 0 || $content{$p-1} != "\\") $quotes = false; $pos = $p + 1; if($quotes == false) continue; //todo } if($pos >= $len) break; //we don't have quotes or comments if($quotes == false && $comments == false) { //find which is first $posq1 = stripos($content, '"', $pos); $posq2 = stripos($content, "'", $pos); $posc1 = stripos($content, "//", $pos); $posc2 = stripos($content, "/*", $pos); //we validate values $errors = 0; if($posq1 === false) { $posq1 = $len+1; $errors++; } if($posq2 === false) { $posq2 = $len+1; $errors++; } if($posc1 === false) { $posc1 = $len+1; $errors++; } if($posc2 === false) { $posc2 = $len+1; $errors++; } if($errors < 4) { //if quotes are before if(min($posq1, $posq2) < min($posc1, $posc2)) { $pos = min($posq1, $posq2)+1; $quotes = ($posq1 < $posq2 ? '"' : "'"); } else { //if simple comments are first then ignore everything else after if($posc1 < $posc2) { $content = substr($content, 0, $posc1).substr($content, stripos($content, "\n", $posc1)); $len = strlen($content); $pos = $posc1; } else { //we have allready end quotes if(($p = stripos($content, "*/", $posc2)) !== false) { $content = substr($content, 0, $posc2).substr($content, $p+2); $len = strlen($content); $pos = $posc2; } } } } else { break; } } } return $content; } ?>
codul NU se simplifica prin eliminarea comentariilor. Never EVER.
Nu ai inteles.
Codul se simplifica atunci cand esti nevoit sa interpretezi TU codul, nu Zend-ul. 🙂