Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Infinite loop

Status
Not open for further replies.

kosmokramer

Programmer
Sep 8, 2002
73
US
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:

Code:
<?php

  function writeInfoToString()
  {
    $someContent = implode('', file(&quot;[URL unfurl="true"]http://www.cnn.com/index.html&quot;));[/URL]
    readAndSearchTheString($someContent);
  }
  function readAndSearchTheString($someContent)
  {
    $storyCounter = 0;
    $cnnString = 'cnnMainNewT2';
    $divDivider = '</div>';
    $linkDivider = '<a href=&quot;';
    $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(&quot;<br>$newsStories[$i]<br>&quot;);
    }
  }
?>

when I run the code, the news stories come out in a string similar to this:

Code:
• <a href=&quot;/2003/ALLPOLITICS/12/08/elec04.medicare/index.html&quot;>Medicare bill becomes law</a>
| <a href=&quot;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')&quot;>Interactive</a>
| <img src=&quot;[URL unfurl="true"]http://i.cnn.net/cnn/.element/img/1.0/misc/premium.gif&quot;[/URL] alt=&quot;premium content&quot; width=&quot;9&quot; height=&quot;11&quot; hspace=&quot;0&quot; vspace=&quot;0&quot; border=&quot;0&quot; align=&quot;absmiddle&quot;> <a href=&quot;javascript:LaunchVideo('/politics/2003/12/08/lok.medicare.bill.cnn.','300k');&quot;>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?
 
Do this:

Code:
<?php

  function writeInfoToString()
  {
    $someContent = implode('', file(&quot;[URL unfurl="true"]http://www.cnn.com/index.html&quot;));[/URL]
    readAndSearchTheString($someContent);
  }
  function readAndSearchTheString($someContent)
  {
    $storyCounter = 0;
    $cnnString = 'cnnMainNewT2';
    $divDivider = '</div>';
    $linkDivider = '<a href=&quot;';
    $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 + 1 < strrpos($tempSubstring, '/'))
       {	
           $dividerPos = strpos($tempSubstring,'/',$dividerPos + 1);
           $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(&quot;<br>$newsStories[$i]<br>&quot;);
    }
  }
?>

Hope that helps :)

Take Care,
Mike
 
Thanks for your help. Unfortunately, your method only corrects the first link. I need to search through the whole story for any instance of a link so it can be changed. If you look at the example story listed in my first post, there are three links that have to be corrected. So, what I need to do is loop through the length of the string and insert before the relative link (&quot; goes here.htm&quot; instead of &quot;/something goes here.htm&quot;). If I leave it as is, the link resolves to my domain instead of CNN which will result in a dead link!!
 
Here's the code with the wording link :)

Code:
<?php

  function writeInfoToString()
  {
    $someContent = implode('', file(&quot;[URL unfurl="true"]http://www.cnn.com/index.html&quot;));[/URL]
    readAndSearchTheString($someContent);
  }
  function readAndSearchTheString($someContent)
  {
    $storyCounter = 0;
    $cnnString = 'cnnMainNewT2';
    $divDivider = '</div>';
    $linkDivider = '<a href=&quot;';
    $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 + 1 < strrpos($tempSubstring, '/'))
       {    
           $dividerPos = strpos($tempSubstring,'/',$dividerPos + 1);
           $newTempSubstring = substr_replace($tempSubstring,'[URL unfurl="true"]http://www.cnn.com',$dividerPos,0);[/URL]
       }
       $newsStories[$storyCounter] = str_replace('href=&quot;','href=&quot;[URL unfurl="true"]http://www.cnn.com',[/URL] $newTempSubstring);
       $storyCounter++;
    }
    for($i = 0;$i<$storyCounter;$i++)
    {
       echo(&quot;<br>$newsStories[$i]<br>&quot;);
    }
  }
?>

Take Care,
Mike
 
Hey. Thanks again, but I just figured it out a day or so ago. It isn't quite as elegant as yours, but hey, who needs an elegant solution as long as it works!

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top