[英]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”
<img height="15" alt="" width="12" src="http://www.asite.org/empty.jpg" />
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 : 这是到目前为止我尝试过的一个示例:
/(<img [a-z = ""]* src="http:\/\/www\.asite\.org\/empty\.jpg" \/>)/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">
<img alt="" src="http://asite.org/empty.png" /></span></span> Yes/no&nbsp;<br />
<img alt="" src="http://asite.org/empty.png" /></span></span> Other text/no&nbsp;<br />
</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">
<custom name="my_checkbox"></custom></span></span> Yes/no&nbsp;<br />
<custom name="my_checkbox"></custom></span></span> Other text/no&nbsp;<br />
</literal>
</paragraph>
</section>
</sections>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.