• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

PHP Maximum Execution TIme Exceeded

I have a script that takes as input a file with results from swim meets.  These are generally aroung 500-600 KB and consist of a table with lots of rows.

The script cleans up the file input so that it looks like the rest of the swim team's site.  The file upload takes between 25 - 40 seconds depending on size, and the processing can take anywhere from 25-60 seconds or so.  As I own the server, it's no problem editing the php.ini file to up the time limits, but, based on the code below, can anyone offer some ideas to clean up the preg_replace statements and speed up the processing?

Thanks.

Alan

====================================================================================

// make sure meet exists
      $good_meet_sql =  mysql_query ("SELECT m_name FROM team_meets WHERE m_id='$mid' AND m_cid='$cid' LIMIT 1");
      if (@mysql_num_rows($good_meet_sql) > 0) { $good_meet='1'; }
      else { $good_meet='0'; }
      if ($good_meet=='0') {
            print '<p class="message">The requested meet could not be located in the database.</p>';
            }
      else {
            while ($row = @mysql_fetch_array($good_meet_sql)) {
                  extract($row);
                  }
            if(isset($_POST['upload']) && $_FILES['resultsfile']['size'] > 0) {
                  $fileName = $_FILES['resultsfile']['name'];
                  $tmpName  = $_FILES['resultsfile']['tmp_name'];
                  $fileSize = $_FILES['resultsfile']['size'];
                  $fileType = $_FILES['resultsfile']['type'];
                  $extension = file_extension($fileName);
                  if (($extension != 'htm')&&($extension != 'html')&&($extension != 'HTM')&&($extension != 'HTML')) {
                        print '<p class="message">The uploaded results file was not the proper file format.</p>';
                        }
                  else {
                        $fp      = fopen($tmpName, 'r');
                        $m_results = fread($fp, filesize($tmpName));
                        fclose($fp);

                        $m = array();
                        preg_match('/<body[^>]*>(.*)<\/body>/is', $m_results, $m);
                        $m_results=$m[1];
                        $to_replace=array('\'','<td ','<TD ','<td>','<TD>','<B>','<b>','</B>','</b>',' WIDTH=100%','WIDTH=','<TABLE ','<TABLE>','</TABLE>','ALIGN=','BLEEDLEFT','CELLSPACING=','CELLPADDING=','BORDER=','<TR>','</TR>','</TD>','COLSPAN','RIGHT','NOWRAP','<TR Valign=TOP>','class="meetresult" colspan=56','class="meetresult" colspan=50','<table align=bleedleft cellspacing=0 cellpadding=0 border=0>','</table>');
                        $do_replace=array('&apos;','<td class="meetresult" ','<td class="meetresult" ', '<td class="meetresult">','<td class="meetresult">','<span class="bold">','<span class="bold">','</span>','</span>','','width=','<table ','<table>','</table>','align=','bleedleft','cellspacing=','cellpadding=','border=','<tr>','</tr>','</td>','colspan','right','nowrap','<tr valign=top>','class="meetresult" align="center" height=20 valign=middle colspan=56 nowrap','class="meetresult" align="center" height=20 valign=middle colspan=56 nowrap','','');
                        $m_results=str_replace($to_replace, $do_replace, $m_results);
                        $m_results=preg_replace('/<tr valign=top[^>]*>(.*)Licensed To:(.*)<\/tr>(.*)<\/tr>(.*)<\/tr>/isU','',$m_results);
                        $m_results=preg_replace('/<FONT[^>]*>/','',$m_results);
                        $m_results=preg_replace('/<\/FONT>/','',$m_results);
                        $m_results=preg_replace('/<td class="meetresult" colspan=57 nowrap><span class="bold"> <\/span><\/td>/','<td class="meetresult" colspan=57 height=10 valign=middle nowrap><span class="bold"> </span></td>',$m_results);
                        $m_results=preg_replace('/<td class="meetresult" align=CENTER colspan=57 nowrap>/','<td class="meetresult" align="center" colspan=57 height=20 valign=middle nowrap>',$m_results);
                        $m_results='<table align=left cellspacing=0 cellpadding=0 border=0>'.$m_results.'</table>';
                      $sql = mysql_query("UPDATE team_meets SET m_results='$m_results' WHERE m_id='$mid' AND m_cid='$cid'");
                      if(!$sql) {      print '<p class="message">There was a problem uploading the results file.</p>'; }
                        else {
// log entry
                              $upload_log="$this_full_name uploaded the file $fileName to the $m_name meet results.";
                              $sql = mysql_query("INSERT INTO team_log (l_userid, l_datetime, l_unixtime, l_activity, ip, host, agent) VALUES('$this_userid', '$timestamp', '$unixtime', '$upload_log', '$ip', '$visitor_host', '$agent')");
// display result
                              print '<p class="text">The results file <span class="bold">'.$fileName.'</span> was successfully uploaded to the '.$m_name.' meet.</p>';
                              }
                        }
                  }
            else {
                  print '<p class="message">There was a problem uploading the results file.</p>';
                  }
            }
0
alanpollenz
Asked:
alanpollenz
  • 2
  • 2
1 Solution
 
hernst42Commented:
Instead of using preg_replace you can use str_replace for as they don't include wildcards to match:
$m_results=preg_replace('/<\/FONT>/','',$m_results);
$m_results=preg_replace('/<td class="meetresult" colspan=57 nowrap><span class="bold"> <\/span><\/td>/','<td class="meetresult" colspan=57 height=10 valign=middle nowrap><span class="bold"> </span></td>',$m_results);
$m_results=preg_replace('/<td class="meetresult" align=CENTER colspan=57 nowrap>/','<td class="meetresult" align="center" colspan=57 height=20 valign=middle nowrap>',$m_results);

instead of using str_replace try using strtr

Guess the regexp which can take long is:
$m_results=preg_replace('/<tr valign=top[^>]*>(.*)Licensed To:(.*)<\/tr>(.*)<\/tr>(.*)<\/tr>/isU','',$m_results);
0
 
alanpollenzAuthor Commented:
hernst42 wrote:

Guess the regexp which can take long is:
$m_results=preg_replace('/<tr valign=top[^>]*>(.*)Licensed To:(.*)<\/tr>(.*)<\/tr>(.*)<\/tr>/isU','',$m_results);

YES!!! I commented that out and the actual processing time dropped to under 1 second.  Why does that statement take so long to process, and what can I do about it?

Note that the actual line of html being replaced in this case is
--------------------------------------------------------------------------------

<tr valign=top>
<td class="meetresult" colspan=34 nowrap>Licensed To: New Wave Swim Team</td>
<td class="meetresult" colspan=6> </td>
<td class="meetresult" align=right colspan=4 nowrap>10/16/2007</td>
<td class="meetresult" colspan=2> </td>
<td class="meetresult" colspan=3>Page</td>
<td class="meetresult" align=right colspan=4 nowrap>1</td>
</tr>

--------------------------------------------------------------------------------

and that it only occurs one time very close to the top of the input file.  The team name, in this case New Wave Swim Team, changes depending on the team using the code.

Is there a way to have the troublesome preg_replace statement only process until the first match and then stop?

Thanks.

Alan
0
 
hernst42Commented:
Then why not use:

$m_results=preg_replace('/<tr valign=top[^>]*>(.*)Licensed To:(.*)<\/tr>/isU','',$m_results);

The U it will stop at he first closing </tr>
0
 
alanpollenzAuthor Commented:
Well...I want the next 2 lines following the html above to be replaced as well.  Those 2 lines are:

---------------------------------------------------------------------------------------------------------------------

<tr valign=top>
<td class="meetresult" align="center" colspan=57 height=20 valign=middle nowrap><span class="bold">WAVE SWIMMING</span></td>

</tr>
<tr valign=top>
<td class="meetresult" align="center" colspan=57 height=20 valign=middle nowrap><span class="bold">Individual Meet Results</span></td>
</tr>

---------------------------------------------------------------------------------------------------------------------

and the 'WAVE SWIMMING' will change based on the team using the software.

Wouldn't the original code I had:

$m_results=preg_replace('/<tr valign=top[^>]*>(.*)Licensed To:(.*)<\/tr>(.*)<\/tr>(.*)<\/tr>/isU','',$m_results);

stop after the 3rd closing </tr> and the U stop it after the first match?

alan
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now