0

I have an Xml content that i want to modify before using the eZ Publish 5 API to create it.

I am trying to implement a Regex to modify the content.

Here is the Xml code that i have (with html entities) :

Print of Xml code http://img15.hostingpics.net/pics/453268xmlcode.jpg

I want to be able to catch empty.jpg in :

<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"

&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.

The aim would be :

Xml code - Aim 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
Kabylzki
  • 37
  • 7
  • Showing us an example of what you've tried so far (that didn't work) may be helpful since it'd let us see what sort of approach you are trying. Also this: http://stackoverflow.com/a/1732454/945456 – Jeff B Nov 12 '15 at 16:08
  • 1
    Example added, thanks for the link. – Kabylzki Nov 12 '15 at 16:41

1 Answers1

0

Dealing with XML i've used an XML parser to reach the desired section.

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).

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

Giuseppe Ricupero
  • 6,134
  • 3
  • 23
  • 32
  • Alright i'll try to implement your solution this morning i'll come back to you ASAP ! – Kabylzki Nov 13 '15 at 08:54
  • Th reaseon why i said i have to match "empty.jpg" is because i want to do it for other images. – Kabylzki Nov 13 '15 at 09:13
  • Matching with the span is a good idea, but I have a lots of xml objects that are not always formed like this one but have an image to be replaced. I think we can just treat the xml as simple text, the ezpublish API will do the rest while saving the object in database. – Kabylzki Nov 13 '15 at 10:18
  • @Kabylzki: xml parser or not, the regex replace the entire img tag with the custom tag, do you want to replace only the image filename? (maintaining the site?) Please provide some extra examples – Giuseppe Ricupero Nov 13 '15 at 16:26
  • Any ways this solution worked for me all i needed to do was to match the image tag to replace it by the custom tag. Thanks. – Kabylzki Nov 16 '15 at 14:47