我有这样的SVG代码字符串:
<g class="highcharts-legend" transform="translate(217,262)">
<g clip-path="url(#highcharts-9)">
<g transform="translate(0,0)">
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>1</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>2</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>3</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>4</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>5</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>6</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>7</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>8</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>9</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>10</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
</g>
</g>
</g>
除前五项外,需要删除所有匹配<g class="highcharts-legend-item">...</g>
。
谢谢你的帮助!
答案 0 :(得分:0)
又快又脏:
(?:<g\ class="highcharts-legend-item">.+?</g>[\n\r]){5}\K
(?:<g\ class="highcharts-legend-item">.+?</g>[\n\r])+
请参阅a demo on regex101.com或(更好)使用解析器。
答案 1 :(得分:0)
我建议使用the DOMDocument
class来处理svg标记。 Here's a fiddle.
<?php
$svg = <<<SVG
<g class="highcharts-legend" transform="translate(217,262)">
<g clip-path="url(#highcharts-9)">
<g transform="translate(0,0)">
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>1</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>2</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>3</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>4</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>5</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>6</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>7</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>8</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>9</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
<g class="highcharts-legend-item"><text x="21" y="15"><tspan>10</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"></rect></g>
</g>
</g>
</g>
SVG;
$dom = new DOMDocument;
$dom->loadXML($svg);
foreach($dom->getElementsByTagName('g') as $g){
if($g->getAttribute("class") === "highcharts-legend-item"){
$items []= $g;
}
}
for($i = 5; $i < count($items); $i++){
$items[$i]->parentNode->removeChild($items[$i]);
}
echo $dom->saveXML($dom->documentElement);
<g class="highcharts-legend" transform="translate(217,262)"> <g clip-path="url(#highcharts-9)"> <g transform="translate(0,0)"> <g class="highcharts-legend-item"><text x="21" y="15"><tspan>1</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"/></g> <g class="highcharts-legend-item"><text x="21" y="15"><tspan>2</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"/></g> <g class="highcharts-legend-item"><text x="21" y="15"><tspan>3</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"/></g> <g class="highcharts-legend-item"><text x="21" y="15"><tspan>4</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"/></g> <g class="highcharts-legend-item"><text x="21" y="15"><tspan>5</tspan></text><rect x="0" y="4" width="16" height="12" fill="#f7a35c"/></g> </g> </g> </g>