The PHP code to remove comments

Test file code is as follows:

? <PHP 

/ ** 
 * PhpStorm the Created by. 
 * the User: Yang 
 * a Date: 2019/10/16 
 * Time: 10:25 
 * / 

// calculated and 
// calculated and 
// calculated and 
$ A =. 1; 
$ B 2 =; 
$ C = $ A $ + B; // sum 

/ * 
 * summation function 
 * / 
function sUM ($ A, $ B) { 

    return $ $ A + B; // return value 
} 

# Note second 
$ . 1 = A; 
$ B = 2; 
## required the product 
$ c = $ a * $ b ; # results 

// special 
$ usedFuncs = "ABCD"; 
tells preg_split ( "IS //", The implode ( "", $ usedFuncs) , -1, PREG_SPLIT_NO_EMPTY);

 

Note removal code is as follows:

/**
 * Created by PhpStorm.
 * User: 25754
 * Date: 2019/10/17
 * Time: 9:54
 */

function removeComment($content)
{
    return preg_replace("/(\/\*(\s|.)*?\*\/)|(\/\/.(\s|.*)\n)|(#(\s*)?(.*))/", '', str_replace(array("\r\n", "\r"), "\n", $content));
}

$content = file_get_contents("./test.php");
echo removeComment($content);

 

Results code is as follows:

$a = 1;
$b = 2;
$c = $a+$b; 

function sum($a, $b) {

    return $a + $b; }


$a = 1;
$b = 2;

$c = $a * $b; 

$usedFuncs = "abcd";
preg_split("

 

Note: The code has //, will be removed

Guess you like

Origin www.cnblogs.com/yang-2018/p/11719426.html