I have a Text.xml file with some text and the bibliographic references in this text. Its look like this:
Text.xml
<p>…blabla S.King (1987). Bla bla bla J.Doe (2001) blabla bla J.Martin (1995) blabla…</p>
And I have a Reference.txt file with list of bibliographic references and ID number for each reference. Its look like this:
Reference.txt
b1#S.King (1987)
b2#J.Doe (2001)
b3#J.Martin (1995)
I would like to find all bibliographic references from Reference.txt into Text.xml and then add a tag with ID. The goal is TextWithReference.xml who must look like this:
TextWithReference.xml
<p>…blabla <ref type="biblio" target=“b1”>S.King (1987)</ref>. Bla bla bla <ref type="biblio" target=“b2”>J.Doe (2001)</ref> blabla bla <ref type="biblio" target=“b3”>J.Martin (1995)</ref> blabla…</p>
To do this, I use a php file.
Search&Replace.php
<?php
$handle = fopen("Reference.txt","r");
while(!feof($handle))
{
$ligne = fgets($handle,1024);
$tabRef[] = $ligne;
}
fclose($handle);
$handleXML = fopen("Text.xml","r");
$fp = fopen("TextWithReference.xml", "w");
while(!feof($handleXML))
{
$ligneXML = fgets($handleXML,2048);
for($i=0;$i<sizeof($tabRef);$i++)
{
$tabSearch = explode('/#/',$tabRef[$i]);
$xmlID = $tabSearch[0];
$searchString = trim($tabSearch[1]);
if(preg_match('/$searchString/',$ligneXML))
{
$ligneXML = preg_replace('/($searchString)/','/<ref type=\"biblio\" target=\"#$xmlID\">\\0</ref>/',$ligneXML);
}
}
fwrite($fp, $ligneXML);
}
fclose($handleXML);
fclose($fp);
?>
The problem is that this php script just copy Text.xml in TextWithReference.xml without identifing the bibliographic references and without adding the tags…
Many thanks for your help!