Re: html parsing

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Mike,

(I know it can probably be done better, or more elegant).

The problem is that the recursion doesn't seem to stop. (Unless there are html ssi's that "loop") there shouldn't be a problem. It basically just freezes up (probably the recursion not terminating/ falling back.) However the html just displays fine when I don't use recursion, so t somewhat baffled me that when I use recursion the script doesn't terminate. I did get to see some error msgs about problems with file streams. Maybe it is running out of file handles ?

thanks,

Ron


Ford, Mike wrote:
On 08 November 2007 06:41, Ron Croonenberg wrote:

ok I wrote something "quick and dirty" real quick:

But somehow it doesn't seem to like recursion. Is there something
"special" one needs to do in php ?

Recursive functions work just fine in PHP. What's the error message?

As far as I can see, there's nothing wrong with what you've posted, except for where there's some inefficiency due to not having found the right functions to use (see corrections below).

here's the code snippet:

function parsehtmlline($line)
{
if (strlen(strstr($line, "#include")) == 0 &&
    strlen(strstr($line, "<!--")) == 0) {

  if (strpos($line, "#include")===FALSE && strpos($line, "<!--")===FALSE)

      /* nothing to parse just print it */
      print($line);
   }
else {
   /* extract the filename */
   $ssi = extractHTMLssi($line);

   /* open the file if it exists and output */
   /* it else just print the string         */
   if (file_exists($ssi)) {
      $incfile = fopen($ssi, "r");
      while (!feof($incfile)) {
         $ssiline = fgets($incfile, 1024);
// somehow PhP doesn't really like recursion, needs to be fixed
// for now just print the line.
//         parsehtmlline($ssiline);
         print($ssiline);
         }
      fclose($incfile);
      }
   else
      print($ssi);
   }
}

function extractHTMLssi($line)
{
$ssi = "";
$strptr = strstr($line, "\"");
if (strlen($strptr) == 0)
   return $line;
else
   {
   $ssi=$strptr;
   $iss=strrev($ssi);
   $strptr = strstr($iss, "\"");
   $iss = substr($strptr, 1, -1);
   $ssi = strrev($iss);
   }

return $ssi;

I'd replace the whole of this function body with something like:

  $pos = strpos($line, '"');
  if ($strpos===FALSE):
    return $line;
  else:
    return substr($line, $pos+1, strrpos($line, '"')-1);
  endif;


}

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
JG125, The Headingley Library,
James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: m.ford@xxxxxxxxxxxxxx
Tel: +44 113 812 4730 Fax: +44 113 812 3211

To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm

--

=================================================================
 Ron Croonenberg                   |
                                   | Phone: 1 765 658 4761
 Lab Instructor &                  | Fax:   1 765 658 4732
         Technology Coordinator    |
                                   |
 Department of Computer Science    | e-mail: ronc@xxxxxxxxxx
 DePauw University                 |
 275 Julian Science & Math Center  |
 602 South College Ave.            |
 Greencastle, IN  46135            |
=================================================================

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux