php - 正则表达式获取 XML 标签之间的内容

标签 php regex

使用正则表达式,我想返回 <w:t ...> 之间的所有内容和</w:t>在内容字符串中

我使用的正则表达式如下:#(<w:t[^>]*>)([^<>]*)?</w:t>#

但是,有时内容会在开头被剪切,因此内容可以以 text</w:t> 开头

我想要一个匹配的正则表达式:

  • $1:''
  • $2:'文本'

并且仍然继续匹配 <w:t> 包围的文本

我现在有两个正则表达式(另一个是 #^()([^<]+)# ),但我想将它们放入一个正则表达式中:

示例内容:

 {#fuites}</w:t></w:r><w:r w:rsidR="00E4576A"><w:rPr><w:bCs/></w:rPr><w:t>{</w:t></w:r><w:r w:rsidR="0066055D"><w:rPr><w:bCs/>
</w:rPr><w:t>type</w:t></w:r><w:r w:rsidR="00E4576A">
<w:rPr><w:bCs/></w:rPr><w:t>}</w:t></w:r>
<w:r w:rsidR="00900433"><w:rPr><w:bCs/>
</w:rPr><w:t xml:space="preserve">
</w:t></w:r><w:r w:rsidR="00900433">
<w:rPr><w:noProof/><w:lang w:eastAsia="fr-FR"/>
</w:rPr><w:drawing><wp:inline distT="0" distB="0" distL="0" distR="0" wp14:anchorId="1745F132" wp14:editId="7142C18F">
<wp:extent cx="225632" cy="179073"/><wp:effectExtent l="0" t="0" r="3175" b="0"/>
<wp:docPr id="7" name="Image 7"/><wp:cNvGraphicFramePr><a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/></wp:cNvGraphicFramePr><a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:nvPicPr><pic:cNvPr id="0" name=""/><pic:cNvPicPr/></pic:nvPicPr><pic:blipFill><a:blip r:embed="rId11"/><a:stretch><a:fillRect/></a:stretch></pic:blipFill><pic:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="228029" cy="180975"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></pic:spPr></pic:pic></a:graphicData></a:graphic></wp:inline></w:drawing></w:r><w:r><w:rPr><w:b/><w:bCs/></w:rPr><w:t xml:space="preserve"> {Type}</w:t></w:r><w:r><w:rPr><w:b/><w:bCs/></w:rPr><w:tab/><w:t>{</w:t></w:r><w:r><w:rPr><w:bCs/></w:rPr><w:t>Description}</w:t></w:r></w:p><w:p w:rsidR="00FF7A5E" w:rsidRPr="00FF7A5E" w:rsidRDefault="00FF7A5E" w:rsidP="00CA7135"><w:pPr><w:rPr><w:bCs/></w:rPr></w:pPr><w:r><w:rPr><w:bCs/></w:rPr><w:t>{/fuites}

最佳答案

您需要第二个正则表达式(事实上您还需要更多),因为正则表达式不是一个足够强大的工具来解析 XML/HTML。没有办法构造一个或多个正则表达式来成功地完成您想要的操作。我建议不要这样做,而是 PHP's XML Parser或其他一些支持 XML 的库。

关于php - 正则表达式获取 XML 标签之间的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16815295/

相关文章:

正则表达式 - 匹配主题标签之外的子字符串(#)

python - 谷歌应用程序引擎 webapp WSGIApplication 中 hexdigest 的正则表达式匹配

php - Composer,从/vendor/文件夹移动文件

php - 使用 session_start 两次

php - 在 Prestashop 后台订单表中添加状态字段

php - 服务器负载高时整个页面无法加载,如何修复?

c# - 不在正则表达式中

php mysql + 警告

c++ - 正则表达式 C++ Qt

python - 将地址拆分为三个不同系列的正则表达式 [第 1 部分]