2016-11-20 9 views
-2

を除くすべての一致を削除します。最初の5つを除くすべての試合<g class="highcharts-legend-item">...</g>を削除する必要が私はこのようなSVG-コードの文字列持つ最初の5

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

を。 助けてくれてありがとう!

答えて

0

svgマークアップの使用には、the DOMDocument classを使用することをおすすめします。あなたの推薦のための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> 
+1

ありがとう!それはうまく動作し、追加のコンバージョンに役立ちます。 –

0

クイックや汚れ:

(?:<g\ class="highcharts-legend-item">.+?</g>[\n\r]){5}\K 
(?:<g\ class="highcharts-legend-item">.+?</g>[\n\r])+ 

a demo on regex101.comまたは(より良い)の代わりにパーサーを使用を参照してください。

関連する問題