我有一个带有XML文本字符串的PHP变量.我需要将其顶级标记,例如<;w:p>;和<;w:tbl>;(按现有顺序排列)传递到这样的数组中,而不包含其内容.
具有预期结果的样例array.
- 《魔法师》,《魔法师》
- "一张桌子"
- 《魔法师》,《魔法师》
到目前为止,我已经完成了一个示例PHP代码
<?php
$text= <<<EOT
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14">
<w:body>
<w:p w:rsidR="00FC1847" w:rsidRDefault="00A526BC">
<w:r>
<w:t>foo</w:t>
</w:r>
<w:r w:rsidR="007C7582">
<w:t>0</w:t>
</w:r>
<w:bookmarkStart w:id="0" w:name="_GoBack"/>
<w:bookmarkEnd w:id="0"/>
</w:p>
<w:tbl>
<w:tblPr>
<w:tblStyle w:val="TabloKlavuzu"/>
<w:tblW w:w="0" w:type="auto"/>
<w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/>
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="11329"/>
</w:tblGrid>
<w:tr w:rsidR="00A526BC" w:rsidTr="00A526BC">
<w:tc>
<w:tcPr>
<w:tcW w:w="11329" w:type="dxa"/>
</w:tcPr>
<w:p w:rsidR="00A526BC" w:rsidRDefault="00A526BC">
<w:r>
<w:t>bar</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</w:tbl>
<w:p w:rsidR="00A526BC" w:rsidRDefault="00A526BC">
<w:r>
<w:t>baz</w:t>
</w:r>
</w:p>
<w:sectPr w:rsidR="00A526BC" w:rsidSect="00A526BC">
<w:pgSz w:w="11907" w:h="16839" w:code="9"/>
<w:pgMar w:top="459" w:right="284" w:bottom="1418" w:left="284" w:header="709" w:footer="709" w:gutter="0"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>
EOT;
preg_match_all('%<w:p .*?>(.*?<w:r>.*?</w:r>).*?</w:p>%si', $text, $matches);
print_r($matches[1]);
这导致了
Array
(
[0] => <w:r><w:t>foo</w:t></w:r>
[1] => <w:r><w:t>bar</w:t></w:r>
[2] => <w:r><w:t>baz</w:t></w:r>
)