p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: loop hangs on second try through


Message #1 by spam@k... on Thu, 3 Oct 2002 19:37:31 -0500
The following function works fine if I only allow the loop to run once. But if I set $numOfMatches=2,
then I get "Fatal error: Maximum execution time of 30 seconds exceeded "Fatal error: Maximum execution time of 30 seconds exceeded".
This seems unlikely, I bet PHP can do hundreds of substr() calls in 30 seconds. It can certainly handle 2. 

Can anyone see where I'm going wrong? This function is being fed a page and it is going through and pulling out all the <a>
tags. (And my server is running PHP 4.01, that is why I'm not using a nice modern function like file_get_contents() .)












function getUrlArray($targetPage, $visibleLinkText="") {
	$fileString = "";
	// get a web page into an array and print it out
	$fcontents = file($targetPage);
	for ($i=0; $i < count($fcontents); $i++) {
	    $fileString .= $fcontents[$i]; 
	}
	
	//$numOfMatches = substr_count($fileString, $visibleLinkText);
$numOfMatches=2;
	
	for ($r=0; $r < $numOfMatches; $r++) {
		// $position gets the position of the first visible letter of the link. We 
		// subtract one to make sure we only get what's in the <a> tag.
		$position = strpos($fileString, $visibleLinkText);
		$position--;
	echo $position;
		for ($i=0; $urlString[$i-1] != "<"; $i++) { 
			$urlString[$i] = substr($fileString, $position, 1);
			$position--; 
			// This next line is to catch text matches that have no actual url enwrapping them. 
			if ($i === 400) $urlString[$i] = "<";
		}	
		$urlString = array_reverse($urlString);
		$urlString = implode("", $urlString);
		$arrayOfAllUrls[] = $urlString;

		// We need to move $position back to the most recent occurence of $visibleLinkText,
		// and then go one beyond that to make sure the next time we cycle through we are 
		// not grabbing the same link, again and again. There is probably a more graceful 
		// to do this. 
		$position = strpos($fileString, $visibleLinkText);
		$position = $position + 1;
		$fileString = substr($fileString, $position);
		$fileString = substr($fileString, 0, 20);
		echo $fileString;
		echo "<BR><BR><BR>";
	}
	
	// We're returning a bunch of full <a> tags with the link info inside them.
	return $arrayOfAllUrls;
}
		





Message #2 by "Nikolai Devereaux" <yomama@u...> on Thu, 3 Oct 2002 19:04:04 -0700
I don't have that much time tonight to run through it all, but I'd throw in an
extra echo statement in your inner loop.  My guess is that the modifications
you make to $position at the end of the first iteration of the outer loop cause
the inner loop to iterate infinitely.

Here's my suggested additions:


>for ($r=0; $r < $numOfMatches; $r++) {
    echo "Outer loop, \$r = $r <br>\n";
    ...
>    for ($i=0; $urlString[$i-1] != "<"; $i++) {
         echo "&nbsp; &nbsp; Inner loop, \$i = $i <br>\n";
         ...


take care,

nik

Message #3 by "Lawrence" <spam@k...> on Thu, 3 Oct 2002 23:11:37 -0400
Bingo. As always, Nik, thanks a million. 






>>>>>>>>>>
Lawrence Krubner
http://www.krubner.com
AOL/Netscape IM: lkrubner
>>>>>>>>>>





----- Original Message ----- 
From: "Nikolai Devereaux" <yomama@u...>
To: "beginning php" <beginning_php@p...>
Sent: Thursday, October 03, 2002 10:04 PM
Subject: [beginning_php] RE: loop hangs on second try through


> 
> I don't have that much time tonight to run through it all, but I'd throw in an
> extra echo statement in your inner loop.  My guess is that the modifications
> you make to $position at the end of the first iteration of the outer loop cause
> the inner loop to iterate infinitely.
> 
> Here's my suggested additions:
> 
> 
> >for ($r=0; $r < $numOfMatches; $r++) {
>     echo "Outer loop, \$r = $r <br>\n";
>     ...
> >    for ($i=0; $urlString[$i-1] != "<"; $i++) {
>          echo "&nbsp; &nbsp; Inner loop, \$i = $i <br>\n";
>          ...
> 
> 
> take care,
> 
> nik
> 
> 


  Return to Index