top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to strip out // comments but not http:// url in PHP

+1 vote
434 views

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

function bookmarksite(title,url){
 if (window.sidebar) // firefox
 window.sidebar.addPanel(title, url, "");
 else if(window.opera &
 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-except-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

posted May 28, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

We have been using a native jsmin extension http://www.ypass.net/software/php_jsmin/ which does a lot more without any trouble for over two years now. It's much faster than the equivalent PHP solution and is probably tested by a lot more people than a home-grown version. You might want to check it out before going too far down this path.

answer May 28, 2013 by anonymous
I appreciate the pointer, but our files, like many people, is a mixture of
HTML, PHP and JS in one file. This jsmin appears to only work on .js files
right? Also, everything else works great in our minifing method, just this
edge case.
My bad. We moved all our JS and CSS to external files for this very reason and created a plugin to combine and minify everything needed by each page into a unique file. While it does cause some duplication across pages, it means that every page loads exactly one JS and CSS file each.

For your situation, I don't know a regular expression that will work in all cases.

Similar Questions
+1 vote

enter image description here

This pic is what i am geting error response from server(HTTP 406). Actually i am trying to post some content from CKeditor, but normal text with styles type of content is accepting by server when i add table content server not accepting.

0 votes

Why ${0} is valid but ${0 is not valid? it means that curly braces evaluate what's inside them and that's why we
cannot have ${0?

Variables shouldn't start from 0 so it means that it started from braces? if yes why I cannot have ${0? and what is the variable here finally? $0 is what it interprets? if yes so it is not a legal variable.

+2 votes

I want to build a A URL Shortener service in my PHP based Website without use of any thirdparty tool. Any clue or opensource would be a great help?

+2 votes

Hi,I have a url some thing like this https://google.com/.I need to replace end of slash in the url.

I need url like this https://google.com.

0 votes

I would like to know how can I pass a parameter via URL using control value on the form.
Something like myPage.php?MyID=txtMyID.value
I can use myPage.php?MyID=1, but cannot use myPage.php?MyID=txtMyID.value.

...