0

I want to match the NEXT occurrence of '/</\g>/' after every '/<g>/'.

I'm trying to use this in a PHP preg_replace to remove any group tags <g> that have no id. I cannot select every closing group tag because I'm not removing all group tags.

For example:

<g id="someid">
  <g>
    <!--content-->
  </g>
</g>

In the above example, the opening group tag needs to be kept, and so does the last </g> that will close the opening tag. The group tag (opening and close) in between needs to go, but not the content inside it.

CaribouCode
  • 13,998
  • 28
  • 102
  • 174
  • 2
    Never ever try to [process XML with regexes](http://stackoverflow.com/a/1732454/420851)! – jasso Apr 08 '14 at 22:16

2 Answers2

2

Better to use DOM parser for this deletion:

$xml = '<g id="someID">
  <g>
    <path d="..." />
  </g>
</g>';
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadXML($xml); // loads your xml
$xpath = new DOMXPath($doc);
$nlist = $xpath->query("//g[not(@id)]"); // g tag without @id

$numnodes = $nlist->length;
for($i=0; $i < $numnodes; $i++) {
   $node = $nlist->item($i);
   $node->parentNode->removeChild($node);
}

$newXML =  $doc->saveXML();
echo $newXML;

OUTPUT:

<g id="someID">
</g>
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks, I can see this may be a better method now. However, until I have the time to re-write my SVG optimizer using this method, I'd like to put this last piece of the puzzle in using `preg_replace` still. – CaribouCode Apr 08 '14 at 21:26
  • If you have code in PHP you can very well use this snippet (no idea what is SVG optimizer) – anubhava Apr 08 '14 at 21:31
  • Tried this method and it deleted the content inside the group tag to be kept. It also added html and body tags. The code is XML (part of an SVG file) so this is no good. – CaribouCode Apr 08 '14 at 21:38
  • What is your expected output from your posted XML sample? – anubhava Apr 08 '14 at 21:43
0

It would be better to use a HTML parser instead of regex to do this kind of operation. Is there a good reason why you're using preg_replace?

Mike Monteith
  • 521
  • 1
  • 6
  • 18