简体   繁体   English

正则表达式-替换内容-eZ发布XML字段

[英]Regex - Replacing content - eZ Publish XML field

I have an Xml content that i want to modify before using the eZ Publish 5 API to create it. 我具有要使用eZ Publish 5 API创建的Xml内容。

I am trying to implement a Regex to modify the content. 我正在尝试实现一个正则表达式来修改内容。

Here is the Xml code that i have (with html entities) : 这是我拥有的Xml代码(带有html实体):

Print of Xml code http://img15.hostingpics.net/pics/453268xmlcode.jpg Xml代码的打印http://img15.hostingpics.net/pics/453268xmlcode.jpg

I want to be able to catch empty.jpg in : 我希望能够在中捕获empty.jpg

<img alt="" src="http://www.asite.org/empty.jpg" />

And replace the whole line for each occurrence by : 为每个事件替换整行:

<custom name="my_checkbox"></custom>

Problem : 问题:

The img tag can sometimes contain other attributes like : height="15" width="12" img标签有时可以包含其他属性,例如: height =“ 15” width =“ 12”

&lt;img height="15" alt="" width="12" src="http://www.asite.org/empty.jpg" /&gt;

And sometimes the attributes are after the src attribute in a different order. 有时,这些属性以不同的顺序位于src属性之后。

The aim would be : 目的是:

Xml code - Aim http://img15.hostingpics.net/pics/318980xmlcodeaim.jpg XML代码-目标http://img15.hostingpics.net/pics/318980xmlcodeaim.jpg

I've tried many things so far but nothing worked. 到目前为止,我已经尝试了很多方法,但是没有任何效果。

Thanks in advance for helping. 在此先感谢您的帮助。

Cheers ! 干杯!

EDIT : 编辑:

Here is an example of what i've tried so far : 这是到目前为止我尝试过的一个示例:

/(&lt;img [a-z = ""]* src="http:\/\/www\.asite\.org\/empty\.jpg" \/&gt)/g

Dealing with XML i've used an XML parser to reach the desired section. 处理XML我已经使用XML解析器来到达所需的部分。

Then we can apply a regex ( ~<img.*?>(?=</span)~ ) to select and replace the image tag with your custom tag (note that in the object received by the xml parser the html entities are replaces with their equivalent char). 然后,我们可以应用正则表达式( ~<img.*?>(?=</span)~ )来选择图像标记并将其替换为自定义标记(请注意,在xml解析器接收的对象中,html实体将被替换及其等价的字符)。

This is a piece of code that emulates and handle your situation: 这是一段模拟和处理您的情况的代码:

<?php
$xmlstr = <<<XML
<sections>
  <section>
    <paragraph>
      <literal class="html">
        &lt;img alt="" src="http://asite.org/empty.png" /&gt;&lt;/span&gt;&lt;/span&gt; Yes/no&amp;nbsp;&lt;br /&gt;
        &lt;img alt="" src="http://asite.org/empty.png" /&gt;&lt;/span&gt;&lt;/span&gt; Other text/no&amp;nbsp;&lt;br /&gt;
      </literal>
    </paragraph>
  </section>
</sections>
XML;

$sections = new SimpleXMLElement($xmlstr);

foreach ($sections->section->paragraph as $paragraph) {
  $re = "~<img.*?>(?=</span)~";
  $subst = "<custom name=\"my_checkbox\"></custom>";
  $paragraph->literal = preg_replace($re, $subst, $paragraph->literal);
}

echo $sections->asXML();

?>

The output is: 输出为:

<?xml version="1.0"?>
<sections>
  <section>
    <paragraph>
      <literal class="html">
        &lt;custom name="my_checkbox"&gt;&lt;/custom&gt;&lt;/span&gt;&lt;/span&gt; Yes/no&amp;nbsp;&lt;br /&gt;
        &lt;custom name="my_checkbox"&gt;&lt;/custom&gt;&lt;/span&gt;&lt;/span&gt; Other text/no&amp;nbsp;&lt;br /&gt;
      </literal>
    </paragraph>
  </section>
</sections>

An online demo can be found HERE 可以在此处找到在线演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM