Mombu the Php Forum

Go Back   Mombu the Php Forum > Php > I can't make 'read_tag.php' file
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 2nd July 03:18
manu
External User
 
Posts: 1
Default I can't make 'read_tag.php' file



Try this:

function readTag($filenane, $tagtype, $degub = false)
// I prefer boolean for debug
{
$filedata = file_get_contents($filename);
$tagtype = preg_quote($tagtype);
$tagRegExp = "/<battag=$tagtype\s*>((?:.|\s)*?)</battag\s*>/";
preg_replace_callback($tagRegExp, 'replaceFunc', $filedata);
}

function replaceFunc(match)
{
// match[0] --- the whole tag from <x> to </x>
// match[1] --- the contents of the tag
// I am my self !!! in the example


return WHATEVER_YOU_WANT_TO_REPLACE_THE_TAG;
}

I didnt make sure this script will work 100%; but it should.

Manu.
  Reply With Quote


 


2 2nd July 03:19
php-general
External User
 
Posts: 1
Default I can't make 'read_tag.php' file



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.

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/
  Reply With Quote
3 2nd July 03:21
bas_timmer49
External User
 
Posts: 1
Default I can't make 'read_tag.php' file


This just says:

Parse error: parse error, unexpected T_STRING, expecting ')' in
C:\pub\read-tags.php on line 12
  Reply With Quote
4 2nd July 03:21
bas_timmer49
External User
 
Posts: 1
Default I can't make 'read_tag.php' file


This works!!! I am a newbie to PHP and i8 knop really nothing about Regular
Expressions!!! Thanks!!!
  Reply With Quote
Reply


Thread Tools
Display Modes




666