note 105943 deleted from function.preg-replace by cmb
| From: | cmb@php.net | Date: | Sun, 29 Sep 2019 07:19:15 +0000 |
| Subject: | note 105943 deleted from function.preg-replace by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212393@lists.php.net to get a copy of this message | ||
Note Submitter: erik dot stetina at gmail dot com
----
simple function to remove comments from string
<?php
function remove_comments( & $string )
{
$string = preg_replace("%(#|;|(//)).*%","",$string);
$string = preg_replace("%/\*(?:(?!\*/).)*\*/%s","",$string); // google for
negative lookahead
return $string;
}
?>
USAGE:
<?php
$config = file_get_contents("config.cfg");
print "before:".$config;
remove_comments($config);
print "after:".$config;
?>
OUTPUT:
before:
/*
* this is config file
*/
; logdir
LOGDIR ./log/
// logfile
LOGFILE main.log
# loglevel
LOGLEVEL 3
after:
LOGDIR ./log/
LOGFILE main.log
LOGLEVEL 3