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

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

 



On Wed, May 29, 2013 at 9:57 AM, Jonesy <gmane@xxxxxxxx> wrote:
> On Tue, 28 May 2013 14:17:06 -0700, Daevid Vincent wrote:
>> 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...
>
> KISS.
>
> To make it simple, straight-forward, and understandable next year when I
> have to re-read what I've written:
>
> I'd change all "://" to "QqQ"  -- or any unlikely text string.
>
> Then I'd do whatever needs to be done to the "//" occurances.
>
> Finally, I'd change all "QqQ" back to "://".
>
> Jonesy

Wow. This is just a spectacularly bad suggestion.

First off, this task is probably a bit beyond the capabilities of a
regex. Yes, you may be able to come up with something that works 99%
of the time, but this is really a job for a parser of some sort. I'm
sorry I don't have any suggestions on exactly where to go with that,
however I'm sure Google can be of assistance. The main problem is that
regex doesn't understand context. It just blindly finds patterns. A
parser understands context, and can figure out which //'s are comments
and which are something else. As a bonus, it can probably understand
other forms of comments like /* */, which regex would completely die
on.

Blindly replacing a string with "any unlikely text string" is just
bad. I don't care how unlikely your text string is, it _will_
eventually show up in a page. It may take 5 years, but it'll happen.
And when it does, this little hack will blow up spectacularly.

I'm sorry to rain on your parade, but this is not KISS. This may seem
simple, but the submarine bugs it introduces will be a nightmare to
track down, and then you'll be in the same boat that you are in right
now. Don't do that to yourself. Do it right the first time.


-- 
--Zootboy

Sent from some sort of computing device.

-- 
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