删除除前五个之外的所有匹配项
Remove all matches except first five
我有这样的 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>
。
感谢您的帮助!
快速而肮脏:
(?:<g\ class="highcharts-legend-item">.+?</g>[\n\r]){5}\K
(?:<g\ class="highcharts-legend-item">.+?</g>[\n\r])+
参见 a demo on regex101.com 或(更好)改用解析器。
我建议使用 the DOMDocument
class for working with svg markup. 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>
我有这样的 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>
。
感谢您的帮助!
快速而肮脏:
(?:<g\ class="highcharts-legend-item">.+?</g>[\n\r]){5}\K
(?:<g\ class="highcharts-legend-item">.+?</g>[\n\r])+
参见 a demo on regex101.com 或(更好)改用解析器。
我建议使用 the DOMDocument
class for working with svg markup. 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>