Forum Moderators: coopster

Message Too Old, No Replies

strip css comments with preg_replace or something else.

         

BlackDex

11:14 am on Jan 30, 2006 (gmt 0)

10+ Year Member



Hello there, i need some help to strip out comments from an css/js file.

I want to use preg_replace (or something else if that is faster ofcourse).

I want to replace single and multi line comments.

so comments like this:


// singel line

/* single line comment */

/**
* multi line
* comment
**/

Also, i want some comment to be in place.
/*<![CDATA[*/ at the top of the page.
and
/*]]>*/ at the bottom of the page.

If this isn't good to do, then i can just add those myself at the top and bottom ofcourse :).

thx in advanced.

coopster

6:28 pm on Jan 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What is your best attempt so far, BlackDex? You know, sooner or later you are going to have to take a good shot at these first ;)

Have you considered strip_tags [php.net] and the optional

allowable_tags
argument?

BlackDex

6:41 pm on Jan 30, 2006 (gmt 0)

10+ Year Member



Hello Coopster,

I Think i have found it.

preg_replace('/(\/\*[\s\S]*?\*\/¦[\r]¦[\n]¦[\r\n])/', '', $buffer)

Thisway it removes all the comments, including the new-lines and puts everything close together.

The CSS still works like a charm :), and with the gz_handler it has become very small now :).

I Whas searching for this a long time, so that is why i asked here, but now i figured it out.

any comments are nice if there are any :).

coopster

7:03 pm on Jan 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Looks pretty good to me. I was just wondering if you really needed that question mark in there though (?), unless I am missing something. Also, at this point I would assume you decided to put the CDATA structures back in afterward as you proposed you might do?

BlackDex

7:21 pm on Jan 30, 2006 (gmt 0)

10+ Year Member



Hmm...
Don't know why i did that ether.. the? that is..

And i indeed put it after the replace.
This saves time also, and i want it to be there always, this whas the best way :).

Thx.

coopster

8:56 pm on Jan 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Nevermind on the question-mark question -- I think I see why you did that now, you are curbing the quantifiers "greediness". I was playing around a bit as I noticed you were not matching the

// single line

comments in your regular expression pattern. I thought I would throw that in for good measure and while I was at it I took a stab at the CDATA comments. You'll notice that I used the .* notation and curbed the greediness by adding a "?" modifier just as you did. The only difference you see now though is that the pattern will indeed match -- even on newlines -- because I added the ("s") modifier to the end of the regular expression. I'm wondering if you didn't have it that way before and weren't getting what you expected ...? Here nor there, if you were, that is why.

$pattern = '/(\/\*(?!(<!\[CDATA¦]]>)).*?\*\/¦\/\/.*?[\r\n]+¦[\r\n]+)/s'; 
$buffer = preg_replace($pattern, '', $buffer);

I think this should work. It did in some small tests I ran. I have a use for it too ;-)

BlackDex

9:42 am on Jan 31, 2006 (gmt 0)

10+ Year Member



Well, my RegExp isn't that good at all, i figured this out by checking others.
And i indeed forgot the // comment :S.
(So if you have an good tutor site about RegExp).

I use it to send as litle data as possible to the users browser, to safe bandwith afcourse.
I Also gzip the css and js files with the use of the gz_handler :).

I Will try to use your RegExp :).
Ill keep ya posted :).

coopster

12:51 pm on Jan 31, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Learning regular expressions? There are some good resources in our PHP Forum Library [webmasterworld.com] thread about Learning PHP - Books, Tutorials and Online Resources [webmasterworld.com] that should get you up and rolling.

My regex? What do you mean? It's yours! I started with yours and went a little further, that's all ;)

By the way, don't forget if you cut and paste code from this forum you will need to rekey the pipe symbols (¦) as this forum breaks them.

BlackDex

9:39 am on Feb 1, 2006 (gmt 0)

10+ Year Member



Hello, thx, that helped also.

I downloaded a program called 'The Regex Coach', and it did how it is named, it coached me :).

Now i only have one small problem with JS files.

They can have URL's in them, which contains [....]

You see my problem :).

Some sample code:

/**
* Function desciption
**/
function myFunction() {
window.open(/**/"http://www.domain.com/index.php", "", "width=250, height=130");
}

My Regular Expression is the following:

'/\/\*.*?\*\/¦\/\/.*?[\r\n\>]¦\<\!--[\r\n]+¦[\t]+/s'

Is there any way to let it be NOT when it starts with ( and ends with )?

Thx in advanced.

coopster

12:22 am on Feb 2, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Those are called negative assertions. You can see an example in the regex I dropped if you notice the 'CDATA' portion that begins with a '(?!' -- you would just use something similar except substitute the CDATA portion with your 'http:' portion.

DrDoc

4:59 am on Feb 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



CSS files can also have URLs in them :)

BlackDex

8:22 am on Feb 2, 2006 (gmt 0)

10+ Year Member



That is indeed true about CSS, BUT, CSS doesn't have // as comments, so that is why i don't use that

But i can't seem to get it to work :(.

I have this expression:

\/\*.*?\*\/¦\/\/(?!(w)).*?([\r\n])¦[\t]+

This will exclude any "//w" it encounters.
But that isn't what i want.

I tried to use "(?!(.*\)))", but has also no effect.
I Know i probably do something wrong, but i can't figure out what.