Re: [PHP] I can't make 'read_tag.php' file

2003-10-13 Thread Bas
This works!!! I am a newbie to PHP and i8 knop really nothing about Regular
Expressions!!! Thanks!!!
Curt Zirzow [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 * Thus wrote Bas ([EMAIL PROTECTED]):
 
  ---
  And with this, it needs the file 'test.tag'
  ---
  bttag=bassie
  I am myself!!
  /bttag
 [...]
 
  The error is that if i load this, the readTag function returns
everything
  except for the Closing!!!

 Do you mean that all the other tags return the proper content
 except the last one?  I didn't study the code too closely for
 reasons that follow.

 
  What's wrong?

 Glad you asked :)   Here are a couple things I noticed:
   - every call to this function has the overhead of reading the
 file.
   - The parsing is error prone, ie. someone puts: bttag=bassie 

 If its possible I would take a complete different approach at
 parsing the tags.  There are two options that I see right away. Use
 XML as your data format and an xml parser to obtain the values in
 the tags.  Or use preg_match_all.  Here is how I would approach the
 preg_match_all:

 function parseTags($file) {
   /* readfile... here */

   $tag_match = !bttag=(\w*)\s*([^]*)\s*/bttag!is;
   preg_match_all($tag_match, $filedata, $matches);
   for ($i=0; $i count($matches[0]); $i++) {
 $tagname = $matches[1][$i];
 $tags[$tagname] = $matches[2][$i];
   }
   return $tags;
 }

 Then you can use it like so:

 ?php
 $bttags = parseTags('test.tag');
 ?
 HTML
 BODY
 h1Test readTag-functie/h1
 ?php echo $bttags['bassie']; ?
 /body
 /html


 HTH,

 Curt
 -- 
 My PHP key is worn out

   PHP List stats since 1997:
   http://zirzow.dyndns.org/html/mlists/

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



[PHP] I can't make 'read_tag.php' file

2003-10-12 Thread Bas
I have found that this script doesn't work:

read_tag.php

---
?php

function readTag($filename, $tagtype, $debug = 0) {
$filedata = file_get_contents($filename);
$tagrealname = bttag=;
$tagrealname .= $tagtype;
$tagrealname .= ;

$tagdata = stristr($filedata, $tagrealname);
$posofend = strpos($tagdata, /bttag);
$length = strlen($tagdata);
$lengthoftag = strlen($tagrealname);
$lengthofend = strlen(/bttag);
$lengthofstr = $length - $posofend - $lengthoftag;
$returndata = substr($tagdata, $lengthoftag, $lengthofstr);
if ($debug == 1) {
echo brLength =  . $length;
echo brOf Tag =  . $lengthoftag;
echo brOf Str =  . $lengthofstr;
echo brOf End =  . $posofend;
echo brTagData:br . $tagdata;
}
return $returndata;
}
?

HTML
BODY
h1Test readTag-functie/h1
?php echo readTag(test.tag, bassie, 1); ?
/body
/html

---
And with this, it needs the file 'test.tag'
---
bttag=bassie
I am myself!!
/bttag
bttag=test
This is a test!!!
/bttag
bttag=welcome
Welcome!!!
/bttag
bttag=close
Closing!!!
/bttag
---
The first parameter of the readTag function is the filename of the tag file.
The second is the tag to search for an the third is the debug mode.

The error is that if i load this, the readTag function returns everything
except for the Closing!!!

What's wrong?

I'm running Win XP with Apache 2.0.44(Win32) CGI setup and PHP 4.3.3

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



Re: [PHP] I can't make 'read_tag.php' file

2003-10-12 Thread Curt Zirzow
* Thus wrote Bas ([EMAIL PROTECTED]):
 
 ---
 And with this, it needs the file 'test.tag'
 ---
 bttag=bassie
 I am myself!!
 /bttag
[...]
 
 The error is that if i load this, the readTag function returns everything
 except for the Closing!!!

Do you mean that all the other tags return the proper content
except the last one?  I didn't study the code too closely for
reasons that follow.

 
 What's wrong?

Glad you asked :)   Here are a couple things I noticed:
  - every call to this function has the overhead of reading the
file.
  - The parsing is error prone, ie. someone puts: bttag=bassie 

If its possible I would take a complete different approach at
parsing the tags.  There are two options that I see right away. Use
XML as your data format and an xml parser to obtain the values in
the tags.  Or use preg_match_all.  Here is how I would approach the
preg_match_all:

function parseTags($file) {
  /* readfile... here */

  $tag_match = !bttag=(\w*)\s*([^]*)\s*/bttag!is;
  preg_match_all($tag_match, $filedata, $matches);
  for ($i=0; $i count($matches[0]); $i++) {
$tagname = $matches[1][$i];
$tags[$tagname] = $matches[2][$i];
  }
  return $tags;
}

Then you can use it like so:

?php
$bttags = parseTags('test.tag');
?
HTML
BODY
h1Test readTag-functie/h1
?php echo $bttags['bassie']; ?
/body
/html


HTH,

Curt
-- 
My PHP key is worn out

  PHP List stats since 1997: 
  http://zirzow.dyndns.org/html/mlists/

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