kosmokramer
Programmer
I am trying to pull news stories from the CNN website, and display them on my webpage (giving them credit for the stories of course). I have gotten it all to work except for one minor detail. The links are relative to my site rather than the CNN website. So, I have been trying to parse the information to add in the full link and have been running into problems. Here's my code:
when I run the code, the news stories come out in a string similar to this:
I can get the first link to work (without the use of a loop to go through the news story), but when I try to use a loop to search for all other links in the story, it gives me an infinite loop. Any ideas?
Code:
<?php
function writeInfoToString()
{
$someContent = implode('', file("[URL unfurl="true"]http://www.cnn.com/index.html"));[/URL]
readAndSearchTheString($someContent);
}
function readAndSearchTheString($someContent)
{
$storyCounter = 0;
$cnnString = 'cnnMainNewT2';
$divDivider = '</div>';
$linkDivider = '<a href="';
$endLocation = 0;
$newsStories = array();
$location1 = 0;
while($storyCounter<4 && $someContent)
{
$location1 = (strpos($someContent,$cnnString, $endLocation))+14;
$endLocation = strpos($someContent, $divDivider, $location1);
$tempSubstring = trim(substr($someContent,$location1,($endLocation-$location1)));
$dividerPos = 0;
while($dividerPos < strlen($tempSubstring))
{
$hrefLocation = strpos($tempSubstring, $linkDivider, $dividerPos);
$dividerPos = strpos($tempSubstring,'/',$hrefLocation);
$newTempSubstring = substr_replace($tempSubstring,'[URL unfurl="true"]http://www.cnn.com',$dividerPos,0);[/URL]
}
$newsStories[$storyCounter] = $newTempSubstring;
$storyCounter++;
}
for($i = 0;$i<$storyCounter;$i++)
{
echo("<br>$newsStories[$i]<br>");
}
}
?>
when I run the code, the news stories come out in a string similar to this:
Code:
• <a href="/2003/ALLPOLITICS/12/08/elec04.medicare/index.html">Medicare bill becomes law</a>
| <a href="javascript:CNN_openPopup('/interactive/allpolitics/0311/medicare.plans/frameset.exclude.html','620x430','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=620,height=430')">Interactive</a>
| <img src="[URL unfurl="true"]http://i.cnn.net/cnn/.element/img/1.0/misc/premium.gif"[/URL] alt="premium content" width="9" height="11" hspace="0" vspace="0" border="0" align="absmiddle"> <a href="javascript:LaunchVideo('/politics/2003/12/08/lok.medicare.bill.cnn.','300k');">Video</a><br>
I can get the first link to work (without the use of a loop to go through the news story), but when I try to use a loop to search for all other links in the story, it gives me an infinite loop. Any ideas?