a simple office document with containing 2 pharagraphs and a table between them

我有一个带有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>
)

推荐答案

与使用正则表达式不同,您可以使用DOMDocumentDOMXPath来获取使用XPath表达式/w:document/w:body/*的所有子元素,然后判断例如nodeName:

$dom = new DOMDocument();
$dom->loadXML($text);

$xpath = new DOMXPath($dom);
$elms = [];
foreach ($xpath->query('/w:document/w:body/*') as $node) {
    if ($node->nodeName === "w:p") {
        $elms[] = "a paragraph";
    }
    if ($node->nodeName === "w:tbl") {
        $elms[] = "a table";
    }    
}

print_r($elms);

输出

Array
(
    [0] => a paragraph
    [1] => a table
    [2] => a paragraph
)

PHP demo.

Php相关问答推荐

使用.htaccess将任何路由重定向到文件

RediSearch PHP希望我在每次使用时定义所有字段

PHP原始数据读取引发max_input_vars错误

如何修复警告:当使用WordPress get_the_Terms()时,Foreach()参数必须是ARRAY|OBJECT类型?

用于计算付款费用的WooCommerce管理编辑产品中的自定义复选框

服务器迁移后无法上载文件-可能存在权限问题

根据发货类别在WooCommerce产品页面中显示快捷代码

在Laravel中创建辅助函数时的约定

在WooCommerce中只允许高价产品的BACS付款

在 WooCommerce 中何时何地执行 woocommerce_order_status_completed_notification 挂钩?

在 Woocommerce 邮箱订单中显示产品 GTIN

Woocommerce API - 图像问题

在 WooCommerce 中应用优惠券时显示购物车商品折扣金额

WooCommerce 以编程方式添加的费用不会持续存在

如何限制for循环php中的条目数

WooCommerce 按 ID 限制产品

获取具有所有特定相关模型的模型

如何将 2021-04-08T22:25:09.335541Z 作为时间戳保存到 Laravel 中的数据库中?

页面重新加载后不触发功能 - Wordpress

sweetalert2 ajax 发送 otp 代码并获取 json 响应 php