Forum Moderators: coopster
- <search>
- <searchResult>
<itemNewsgroup></itemNewsgroup>
- <itemTitle></itemTitle>
<itemSize></itemSize>
<itemType></itemType>
<itemLink></itemLink>
</searchResult>
- <searchResult>
<itemNewsgroup></itemNewsgroup>
- <itemTitle></itemTitle>
<itemSize></itemSize>
<itemType></itemType>
<itemLink></itemLink>
</searchResult>
Etc, Etc...... as they are results, it keeps repeating.
All I simply want to get from this file is all the <itemTitle>'s and then display these results in my own php page. This is what I have tried doing:
$data=file_get_contents("http://www.site.com/file.xml");
$itemtitle=preg_match_all("/<itemTitle>(.*?)<\/itemTitle>/", $data, $match);
$title="$match[1]";
echo $title;
It's not working, the echo part returns 'Array' onto the page. So obviously i'm not doing the array thing properly. Any help much appreciated! I'm new to all of this so go easy :)
$data=file_get_contents("http://www.site.com/file.xml");
$itemtitle=preg_match_all("/<itemTitle>([b].+[/b])<\/itemTitle>/", $data, $match);
echo '<pre>';
print_r($match);
echo '</pre>';
$match should be a two-dimensional array. This is why you are echoing 'array' to the browser.
Anyway, goodluck!
Stupid question: Do you have any text between any of the XML tags?
Also, I suggest changing your pattern to:
$pattern = "/<itemTitle>(.+)<\/itemTitle>/";
Otherwise it is going to find a match if there is no text in the tag; it will push an empty array value to $match.
Other than that, your code should work. Again, try using print_r to see what the whole array contains.
Answering your question, yes, the <itemTitle> tags have content in them. When I try using the (.+) instead of (.*?) it prints out the other xml tags which are not needed. Using the (.*?) it is printing out just the content of the <itemTitle> tags... just the weird <![CDATA[]]> thing that is the problem, and obviously the fact that it doesn't echo onto the page because of this.
My new code is this:
$data = file_get_contents("http://www.site.com/file.xml");
$itemtitle = preg_match_all("/<itemTitle>(.*?)<\/itemTitle>/", $data, $match);
foreach ($match[1] as $title)
{
$replace = array('<![CDATA['=>'',']]>'=>'');
$title = strtr($title,$replace);
echo $title . '<br>';
}
Maybe there is a way for it to not output <![CDATA[]]> in the first place?
$display_num = 30;
$data = file_get_contents("http://www.site.com/file.xml");
$itemtitle = preg_match_all("/<itemTitle><!\[CDATA\[(.+)\]\]<\/itemTitle>/", $data, $match);
for($i = 0; $i < $display_num; $i++){
echo $match[1][$i].'<br />';
}
Something like that :)
$data = file_get_contents("http://www.site.com/file.xml");
$itemtitle = preg_match_all("/<itemTitle><!\[CDATA\[(.+)\]\]<\/itemTitle>/", $data, $match);
$count = count($match[1]);
$display_num = ($count < 30)? $count : 30;
for($i = 0; $i < $display_num; $i++){
echo $match[1][$i].'<br />';
}
$itemtitle = preg_match_all("/<itemTitle><!\[CDATA\[(.+)\]\]<\/itemTitle>/", $data, $match); didn't work, it returned no results that way. So I have this:
$itemtitle = preg_match_all("/<itemTitle>(.*?)<\/itemTitle>/", $data, $match); and
$replace = array('<![CDATA['=>'',']]>'=>'');
$match[1][$i] = strtr($match[1][$i],$replace); and it is displaying correctly, and limited to 30 results. Thanks for your help :)
My suggestion for the pattern was to include the extraneous information into it so that you don't have to do a string replace each time you echo it out to the browser. I think it's a better solution just my pattern may be off a little.
Anyway, if what you have suits you then I guess you'll be fine :)