need some regex help to strip out // comments but not http:// urls

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I'm adding some minification to our cache.class.php and am running into an
edge case that is causing me grief.

I want to remove all comments of the // variety, HOWEVER I don't want to
remove URLs...

Given some example text here with carefully crafted cases:

// another comment here
<iframe src="http://foo.com";>
function bookmarksite(title,url){
    if (window.sidebar) // firefox
        window.sidebar.addPanel(title, url, "");
    else if(window.opera && window.print){ // opera
        var elem = document.createElement('a');
        elem.setAttribute('href',url);
        elem.setAttribute('title',title);
        elem.setAttribute('rel','sidebar');
        elem.click();
    } 
    else if(document.all)// ie
        window.external.AddFavorite(url, title);
}

I've tried so many variations and hunted on StackOverflow, Google, etc. and
what would seem like a task already solved, doesn't seem to be.

This is "close", but still matches //foo.com (omitting the : of course)

\s*(?!:)//.*?$   (count it as '/m' multiline)


This ultimately ends up in a PHP line of code like so:

$sBlob = preg_replace("@\s*//.*?$@m",'',$sBlob);

Here are some other links of stuff I've found and tried to experiment with
varying degrees.

http://stackoverflow.com/questions/4568410/match-comments-with-regex-but-not
-inside-a-quote
http://stackoverflow.com/questions/611883/regex-how-to-match-everything-exce
pt-a-particular-pattern
http://stackoverflow.com/questions/11863847/regex-to-match-urls-but-not-urls
-in-hyperlinks
http://stackoverflow.com/questions/643113/regex-to-strip-comments-and-multi-
line-comments-and-empty-lines



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux