Replacing // comments with /* comments */ in PHP

China☆狼群 提交于 2019-12-06 16:56:40

A regexp should do the trick:

$str = preg_replace('_//(.*)$_m', '/*$1*/', $str);

This won't take into account quoted strings - if you're using something crazy like

background-image: url('//my-server/my.jpg');

then it's going to think that's a comment. If this is a problem then you're better off writing a proper parser.

<? preg_replace('#//(.*)$#', '/*$1*/', $cssx); ?>

Greg's expression has two problems: first, 'm' and '$' are superfluous, second it doesn't handle carriage returns correctly (in case your system uses them).

A better expression appears to be

 preg_replace('~//([^\r\n]*)~', '/*$1*/', $str);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!