duan33360 2014-04-08 14:53
浏览 85
已采纳

用于preg_replace HTML注释的正则表达式和SVG文件中的空标记

When Illustrator exports SVG files, it doesn't do a very good job of optimizing them. One annoying and pointless thing it puts in near the top of the file is the following HTML comment:

<!-- Generator: Adobe Illustrator 17.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->

I also have multiple empty group tags with ids like so:

<g id="svgSteps">
</g>
<g id="svgBase">
</g>

Now, I'm trying to write some PHP using regex and preg_replace to remove things like this. I'm completely new to regex and already tried some solutions posted on stackoverflow which didn't work for me.

For the HTML comments I tried:

$fileContent = file_get_contents('my_file');
$fileContent = preg_replace('/<!--(.|\s)*?-->/','',$fileContent);
file_put_contents('my_file',$fileContent);

Which didn't work. When I tried a str_replace for <!-- instead, that worked so I know the file_get_contents and file_put_contents are working (no issues with permissions).

What would be the correct regex for:

  1. Finding HTML comments starting with <!-- and ending with --> that have whitespace, alpha-numeric characters, periods, commas, colons and brackets inside.

  2. Finding tags starting with <g and ending with </g> that can have an id but only have either whitespace or nothing inside the tag.

  • 写回答

3条回答 默认 最新

  • dongzhenju3015 2014-04-08 15:08
    关注

    try

        preg_replace("/((<g id=\".*\">)|(<g>))[\s]*(<\/g>)/",'',$fileContent)
        preg_replace("/(<!--)[\s\S]*(-->)/",'',$fileContent)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度