Forum Moderators: coopster
$data=file_get_contents($xml_file);
$distinctivetitle=preg_match("/<DistinctiveTitle>(.*?)<\/DistinctiveTitle>/", $data, $matchtitle);
$title="$matchtitle[1]";
I also need to get the keywords from an array like the following:
<keywords>
<heading>Keywords</heading>
<keyword>word1</keyword>
<keyword>word2</keyword>
<keyword>word3</keyword>
</keywords>
The following code works with a print_r, but I need the $keywords variable to show word1, word2, word3. The best I can get is word3:
$keyword=preg_match_all("/<keyword>(.*?)<\/keyword>/", $data, $matchkeyword);
$matchkeyword=print_r($matchkeyword, true);
$matchkeyword=($matchkeyword[0][1]);
foreach ($matchkeyword as $value) {
$keywords=$value;
}
I'd like to use simpleXML but the host for this project has 4.3.9 installed.
For more complex XML you will need to parse.
I just happen to have an XML parser script I used for one of my web sites to read an XML feed, it returns an array.. I found the code on php.net (in a user comment) so i cannot take full credit for this code
I included the first few lines of my feed because you will need to modify the code to read your feed (just edit the first line)
Here ya go have fun!
--------------
My xml feed looks like this
<?xml version="1.0" encoding="ISO-8859-1"?>
<Listing>
<system name="Xbox">
<games>
<game>
<title>Need for Speed Underground 2</title>
<url>http://onlycheaters.com/cheat_codes/view_Xbox_Need+for+Speed+Underground+2.html</url>
<hits>2934</hits>
</game>
etc...
etc..
etc..
etc..
etc..
Then I use this code to return an array:
<?
// Remember to put your own values in here (look at my sample XML feed above)
$XML_LIST_ELEMENTS = array( "system", "games", "game" );
function makeXMLTree($file)
{
// read file
$open_file = fopen($file, "r");
$data = "";
while ($r=fread($open_file,8192) ) {
$data .= $r;
}
// create parser
$parser = xml_parser_create();
xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
xml_parse_into_struct($parser,$data,$values,$tags);
xml_parser_free($parser);
// we store our path here
$hash_stack = array();
// this is our target
$ret = array();
foreach ($values as $key => $val) {
switch ($val['type']) {
case 'open':
array_push($hash_stack, $val['tag']);
if (isset($val['attributes']))
$ret = composeArray($ret, $hash_stack, $val['attributes']);
else
$ret = composeArray($ret, $hash_stack);
break;
case 'close':
array_pop($hash_stack);
break;
case 'complete':
array_push($hash_stack, $val['tag']);
$ret = composeArray($ret, $hash_stack, $val['value']);
array_pop($hash_stack);
// handle attributes
if (isset($val['attributes']))
{
while(list($a_k,$a_v) = each($val['attributes']))
{
$hash_stack[] = $val['tag']."_attribute_".$a_k;
$ret = composeArray($ret, $hash_stack, $a_v);
array_pop($hash_stack);
}
}
break;
}
}
return $ret;
}
function &composeArray($array, $elements, $value=array())
{
global $XML_LIST_ELEMENTS;
// get current element
$element = array_shift($elements);
// does the current element refer to a list
if (in_array($element,$XML_LIST_ELEMENTS))
{
// more elements?
if(sizeof($elements) > 0)
{
$array[$element][sizeof($array[$element])-1] = &composeArray($array[$element][sizeof($array[$element])-1], $elements, $value);
}
else // if (is_array($value))
{
$array[$element][sizeof($array[$element])] = $value;
}
}
else
{
// more elements?
if(sizeof($elements) > 0)
{
$array[$element] = &composeArray($array[$element], $elements, $value);
}
else
{
$array[$element] = $value;
}
}
return $array;
}
$res = makeXMLTree("http://onlycheaters.com/feed.xml");
print_r($res);
?>