Re: I can't make 'read_tag.php' file
| From: | Curt Zirzow | Date: | Sun, 12 Oct 2003 15:54:01 +0000 |
| Subject: | Re: I can't make 'read_tag.php' file | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-165904@lists.php.net to get a copy of this message | ||
* Thus wrote Bas (bas_timmer49@hotmail.com):
>
> ---
> 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>
<h1>Test 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/