0

I'm trying to remove the conditional comment statements and any XML content with the conditional statements from a HTML page, I'm using the below PHP but it doesn't seem to be remove the conditional statements and XML content, are the regex statements valid!?

HTML

...
<link rel=File-List href="filelist.xml">
<!--[if gte mso 9]><xml>
 <o:DocumentProperties>
  <o:Revision>3</o:Revision>
  <o:TotalTime>1</o:TotalTime>
  <o:Created>2014-02-14T21:30:00Z</o:Created>
  <o:LastSaved>2014-02-14T21:35:00Z</o:LastSaved>
  <o:Pages>1</o:Pages>
  <o:Words>58</o:Words>
  <o:Characters>331</o:Characters>
  <o:Lines>2</o:Lines>
  <o:Paragraphs>1</o:Paragraphs>
  <o:CharactersWithSpaces>388</o:CharactersWithSpaces>
  <o:Version>15.00</o:Version>
 </o:DocumentProperties>
 <o:OfficeDocumentSettings>
  <o:AllowPNG/>
 </o:OfficeDocumentSettings>
</xml><![endif]-->
<link rel=themeData href="themedata.tx">
...

PHP

$html = preg_replace('/<\?xml[^>]+\/>/im', '', $html);
$html = preg_replace('/<!--\[(.*)\]>/is', '', $html);
$html = preg_replace('/<!\[(.*)\]-->/is', '', $html);
llanato
  • 2,508
  • 6
  • 37
  • 59
  • I think you can try [`'/)[^\]]*)*]-->\R?/'`](https://regex101.com/r/zP7eE5/1). – Wiktor Stribiżew Nov 13 '15 at 14:30
  • Take a look at @lonesomeday answer in the linked question and add a predicate to the XPath query (something like `[starts-with(., '[if ')]` or register your own function like this: http://php.net/manual/en/domxpath.registerphpfunctions.php if you want to be more selective. – Casimir et Hippolyte Nov 13 '15 at 14:40

1 Answers1

0

Try this to match the complete conditional comment:

$html = preg_replace('/<!--\[if gte mso 9\]>.*<!\[endif\]-->/s', '', $html);

http://sandbox.onlinephpfunctions.com/code/e8a48984f34f2323ae14d72b7c33d3065edd00dc

tino.codes
  • 1,512
  • 1
  • 12
  • 23