note 105943 added to function.preg-replace
| From: | erik dot stetina at gmail dot com | Date: | Tue, 27 Sep 2011 10:25:55 +0000 |
| Subject: | note 105943 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183468@lists.php.net to get a copy of this message | ||
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
----
Server IP: 69.147.83.197
Probable Submitter: 92.52.35.25
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- https://master.php.net/note/edit/105943
Del: integrated -- https://master.php.net/note/delete/105943/integrated
Del: useless -- https://master.php.net/note/delete/105943/useless
Del: bad code -- https://master.php.net/note/delete/105943/bad+code
Del: spam -- https://master.php.net/note/delete/105943/spam
Del: non-english -- https://master.php.net/note/delete/105943/non-english
Del: in docs -- https://master.php.net/note/delete/105943/in+docs
Del: other reasons-- https://master.php.net/note/delete/105943
Reject -- https://master.php.net/note/reject/105943
Search -- https://master.php.net/manage/user-notes.php