How you go about it depends on what's inside the loop.
If its a nested loop and the long duration arises due to the number of iterations on the inner loop, then you can detect this in your code e.g.:
foreach ($lines as $line) { $iters=strtok($line, ','); $start_iter=time(); for ($x=0; $x<$iters; $x++) { ... do something here if (time()-$start_iter>600) { print "abandoning $line after $x iterations\n"; break; } } }
OTOH, if the function is essentially atomic, e.g.
foreach ($lines as $line) { mysql_query($line); }
Then control will never return to your code until the operation completes. However that does not mean that its impossible to interrupt the processing and continue to the next line - there are 2 ways to do this:
1) use pcntl_alarm to trigger a signal handler - note that of itself this would not solve the problem in the case above - since on completion of the signal handler, the function called would resume - it might be possible in some case to force premature termination of the lopped function.
2) run the inner part of the loop in a seperate process and kill the process if it overruns.
Have a look at the pcntl functions for more details - note these only work in Linux/Unix/POSIX environments.