Koobi
05-25-2004, 12:39 AM
Well, I've seen some coders around here so...
Ok, I just started with regular expressions a few days ago and I'm trying something out but it doesn't work properly.
I have a PHP file that has single line comments in it in the form //
I'm writing a regexp that removes these comments including the font tags and spaces that wrap them (the argument is the return of highlight_file())
Zero or more + the font tag with one or more spaces and the colour returned from ini_get('highlight.comment') + zero or more + two backslashes + one or more + (any character + zero or more "word" characters + one or more spaces) any number of these
<?php
$contents = highlight_file('someName.class.php', 1);
$pattern = array('/[ ]*<font\s*color="' . ini_get('highlight.comment') . '">[ ]*\/\/[ ]+[\$\*\(\)\.\'\:\[\]\{\}%&-\+=,";\w*\s*]*/i');
$contents = preg_replace($pattern, '', $contents);
echo $contents;
?>
What I need to do is shorten the regexp and add something.
How do I show that I want any character? I've used [\$\*\(\)\.\'\:\[\]\{\}%&-\+=,";\w*\s*]* and I know that this is very inefficient and that there's a shorter way. Can someone tell me what it is?
Right now it removes what I want, but I can't figure out what the newline character is so I don't know how to detect that.
How do I show that I want to detect a new line character? \n?
If anyone knows this, please let me know :)
PS: these are the perl compatible regexp's
Ok, I just started with regular expressions a few days ago and I'm trying something out but it doesn't work properly.
I have a PHP file that has single line comments in it in the form //
I'm writing a regexp that removes these comments including the font tags and spaces that wrap them (the argument is the return of highlight_file())
Zero or more + the font tag with one or more spaces and the colour returned from ini_get('highlight.comment') + zero or more + two backslashes + one or more + (any character + zero or more "word" characters + one or more spaces) any number of these
<?php
$contents = highlight_file('someName.class.php', 1);
$pattern = array('/[ ]*<font\s*color="' . ini_get('highlight.comment') . '">[ ]*\/\/[ ]+[\$\*\(\)\.\'\:\[\]\{\}%&-\+=,";\w*\s*]*/i');
$contents = preg_replace($pattern, '', $contents);
echo $contents;
?>
What I need to do is shorten the regexp and add something.
How do I show that I want any character? I've used [\$\*\(\)\.\'\:\[\]\{\}%&-\+=,";\w*\s*]* and I know that this is very inefficient and that there's a shorter way. Can someone tell me what it is?
Right now it removes what I want, but I can't figure out what the newline character is so I don't know how to detect that.
How do I show that I want to detect a new line character? \n?
If anyone knows this, please let me know :)
PS: these are the perl compatible regexp's