Warning: fopen(graphic_design/files/thread-896-1.txt) [function.fopen]: failed to open stream: Permission denied in /graphic_design/global.php on line 421
file NOT opened Regular experssions, perl style for PHP -
PDA

View Full Version : Regular experssions, perl style for PHP


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('/[&nbsp;]*<font\s*color="' . ini_get('highlight.comment') . '">[&nbsp;]*\/\/[&nbsp;]+[\$\*\(\)\.\'\:\[\]\{\}%&-\+=,";\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

Octane
05-26-2004, 12:38 AM
\n = newline
\r = return (or newline)

as far as accepting any character, I'm pretty sure that it's (.*), where '.' means match any character and the * means to match 0 or more times.

Koobi
05-26-2004, 03:22 PM
Hi Octane,
At the beginning I did consider (.*) but that counts everything following the font tag since there is no limit. I would have to limit it to the next font tag which means I would want to collect everythign in between excpet any X/HTML tags and the next line character and a break tag so I created a pattern for that . Itried this, but it didn't work:

/[&nbsp;]*<font\s?color="' . ini_get('highlight.comment') . '">[&nbsp;]*\/\/[&nbsp;]+[.*^<>]*\n*(<br[\s\/]?>)*[&nbsp;]*<\/font>/i


The reason I need this it to exclude comments for a more clean readable version of a file. This is the result of what I'm using right now:
http://koobi-studio.com/xhtmlValid/removeComments.php

And this is what I'm using:

<?php
$contents = highlight_file('ValidXHTML.class.php', 1);
$pattern = array('/[&nbsp;]*<font\s*color="' . ini_get('highlight.comment') . '">[&nbsp;]*\/\/[&nbsp;]+[\$\*\(\)\.\'\:\[\]\{\}%&\-\+=,";\w*\s*]*/i');
$contents = preg_replace($pattern, '', $contents);
echo $contents;
?>



As you can see that works somewhat but it doens't really work if you look at the HTML output. I would like to do it the right way even though it seems to work fine as far as we can see :)

Thanks for your help so far :)

Octane
05-27-2004, 03:00 AM
what about just collecting everything in between (.*</font>)/i and then just strip the HTML using strip_tags???

Koobi
05-28-2004, 01:38 PM
Hey,
Yeah I could strip_tags as well but I would like to learn how I could do it via regular expressions and if I grab everything in between a closing font tag, won't all the opening font tags go as well?

Octane
05-29-2004, 01:14 AM
yes, i was just shortening it as an example because I'm not that 'fluent' with reg-exp and didn't want to mess you up by giving you an example that doesn't even work ;)