Best way to automatically remove comments from PHP code
I'd use tokenizer. Here's my solution. It should work on both PHP 4 and 5:
$fileStr = file_get_contents('path/to/file');
$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($fileStr);
foreach ($tokens as $token) {
if (is_array($token)) {
if (in_array($token[0], $commentTokens)) {
continue;
}
$token = $token[1];
}
$newStr .= $token;
}
echo $newStr;
Regex to strip comments and multi-line comments and empty lines
$text = preg_replace('!/\*.*?\*/!s', '', $text);
$text = preg_replace('/\n\s*\n/', "\n", $text);
Regex : Remove all comments from html file BUT preserve same number of lines
You may use this search for searching:
(?:^\h*<!--|(?<!\A|-->\n)\G).*\R
and replace that with a "\n"
RegEx Demo
RegEx Details:
(?:
: Start non-capture group^
: Start of a line\h*<!--
: Match 0 or more whitespaces followed by<!--
|
: OR(?<!\A|-->\n)
: Negative lookbehind to avoid match if we have either start position or we have-->
+ line break at previous position\G
: Match end position of previous match
)
: End non-capture group.*\R
: Match remaining characters in line followed by line break
General utility to remove/strip all comments from source code in various languages?
No such tool exists yet.
remove comments from html source code
Try PHP DOM*:
$html = '<html><body><!--a comment--><div>some content</div></body></html>'; // put your cURL result here
$dom = new DOMDocument;
$dom->loadHtml($html);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//comment()') as $comment) {
$comment->parentNode->removeChild($comment);
}
$body = $xpath->query('//body')->item(0);
$newHtml = $body instanceof DOMNode ? $dom->saveXml($body) : 'something failed';
var_dump($newHtml);
Output:
string(36) "<body><div>some content</div></body>"
RegEx to remove /** */ and // ** **// php comments
~//?\s*\*[\s\S]*?\*\s*//?~
This works.Just added another \ and space eaters to your answer.
See demo.
http://regex101.com/r/lK9zP6/6
Related Topics
Generating Cryptographically Secure Tokens
How to Programmatically Login/Authenticate a User
What Do Strict Types Do in PHP
Mock Private Method With PHPunit
Is There Any Particular Difference Between Intval and Casting to Int - '(Int) X'
Replace Last Occurrence of a String in a String
Which Is Faster in PHP, $Array[] = $Value or Array_Push($Array, $Value)
How to Setup Elasticsearch Index Structure With Multiple Entity Bindings
How to Detect a Create, Update, Delete Query Is Successful in Codeigniter
Laravel Migration: Unique Key Is Too Long, Even If Specified
How to Import a .SQL File in MySQL Database Using PHP
How to Override the Path of PHP to Use the Mamp Path
How to Get Last Key in an Array
PHP: If Internet Explorer 6, 7, 8 , or 9