-2

Below is the text I need to remove <w:drawing> tags and its content from

<w:document>
<w:t>some text here</w:t>
<w:drawing>drawing image</w:drawing>
</w:document>

i tried this

$result = preg_replace('/<w:drawing\b[^>]*>(.*?)<\/w:drawing>/i', '', $xml);

but stil getting <w:drawing> tags, any suggestion?

in result i want get

<w:document>
<w:t>some text here</w:t>
</w:document>
miken32
  • 42,008
  • 16
  • 111
  • 154
Arif Nur Rohman
  • 175
  • 1
  • 4
  • 15

2 Answers2

2

What you've got here is not a complete XML document, so I've made some changes to it. Regardless, NEVER try to parse XML with regular expressions. NEVER!!

Here's a quick example using SimpleXML, though DOMDocument would work just as well:

$xml = <<< XML
<?xml version="1.0" encoding="UTF-8"?>
<w:document xmlns:w="w">
    <w:t>some text here</w:t>
    <w:drawing>drawing image</w:drawing>
</w:document>
XML;
$doc = new SimpleXMLElement($xml, 0, false, "w");
$doc->registerXPathNamespace("w", "w");
$drawings = $doc->xpath("//w:drawing");
foreach ($drawings as &$drawing) {
    unset($drawing[0]);
}
$new_xml = $doc->asXML();
echo $new_xml;

Output:

<?xml version="1.0" encoding="UTF-8"?>
<w:document xmlns:w="w">
    <w:t>some text here</w:t>

</w:document>
miken32
  • 42,008
  • 16
  • 111
  • 154
-3

You just need to replace your regex pattern with something like this

$result = preg_replace('/<w:drawing>.*<\/w:drawing>/', '', $xml);
V.Andri
  • 12