preg_replace for changing several things



I hope this is not a repeated question. I want to change following xml code (a small part of xml) to html by using preg_replace. I tried xml parse tools, but I have experienced problems at the rest of the xml, so I try to find a solution like preg_replace.



<contrib-group>
<contrib contrib-type="author">
<name>
<surname>Smith</surname>
<given-names>John</given-names>
</name>
<xref ref-type="aff" rid="aff1">1</xref>
<xref ref-type="corresp" rid="cor1"/>
</contrib>
<contrib contrib-type="author">
<name>
<surname>Hoper</surname>
<given-names>Alfred</given-names>
</name>
<xref ref-type="aff" rid="aff2">2</xref>
</contrib>
<contrib contrib-type="author">
<name>
<surname>Bay</surname>
<given-names>Elisabeth</given-names>
</name>
<xref ref-type="aff" rid="aff3">3</xref>
</contrib>
<contrib contrib-type="author">
<name>
<surname>Back</surname>
<given-names>James</given-names>
</name>
<xref ref-type="aff" rid="aff2">2</xref>
</contrib>
<contrib contrib-type="author">
<name>
<surname>Hampton</surname>
<given-names>George</given-names>
</name>
<xref ref-type="aff" rid="aff4">4</xref>
</contrib>
</contrib-group>


The final text should be like: Smith J1*, Hoper A2, Bay E3, Back J2, Hampton G4


I could change some of them by using the following expressions:



$data = preg_replace('/<xref(.*?)>(.*?)<\/xref>/i', '<sup>$2</sup>', $data);
$data = preg_replace('/<\/contrib>.*?<contrib contrib-type="author">/i', ', ', $data);


But I could not do all of them.


Is it possible ?


No comments:

Post a Comment