I've been pulling my hair out over this for the last few hours so hopefully somebody can help!
I have a text file containing data. I basically need to search the file for a specific set of matching tags (there will be multiple instances of these tags). I then need to grab all the data between them, and place the data into an array.
Here's an example of what I've got so far
data file
<?xml version="1.0" encoding="utf-8"?>
<page>
<static_include>
<head>
</head>
<body>
<contentBlock>
<textBlock>
<pageTitle>
<![CDATA[pagetitle]]>
</pageTitle>
</textBlock>
</contentBlock>
<contentBlock>
<textBlock>
<pageTitle>
<![CDATA[pagetitle]]>
</pageTitle>
</textBlock>
</contentBlock>
</body>
</static_include>
</page>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Regen</title>
<?
$filecontents = file_get_contents("temp.xml");
$targetarray = array();
preg_match_all("/<contentBlock>(.*)<\/contentBlock>/smi", $filecontents , $value);
$value = array_unique($value);
for($i=0; $i<count($value); $i++){
for($j=0; $j<count($value[$i]); $j++){
$targetarray[] = $value[$i][$j];
}
}
print_r($targetarray);
?>
</body>
</html>
This is almost working. The problem I have is that in this example two set of <contentBlock> </contentBlock> tags are found and placed into the same element of the array.
Array
(
[0] => <contentblock>
<textblock>
<pagetitle>
<!--[CDATA[pagetitle]]-->
</pagetitle>
</textblock>
</contentblock>
<contentblock>
<textblock>
<pagetitle>
<!--[CDATA[pagetitle]]-->
</pagetitle>
</textblock>
</contentblock>
)
I want each matched set placed into it's own element.
I really hope somebody can help!!
Thanks