> I've got a little question. I am writing a page where I would like to
> parse my own invented HTML-looking tags (but want to keep the real
HTML
> tags intact). I use a buffer for the output, and just before the end
use
> ob_get_contents() to get the whole buffer which I want to check for
> those tags.
> 
> (Something like:
> 
> $tag->content = ob_get_contents();
> $output = $tag->interpret();
> ob_end_clean();
> echo "$output";
> )
> 
> Now my question is, what is the fastest way to search and replace
> through this file, whereby the interpretation of the tag can be
somewhat
> complicated?
> 
> I first just had a loop running character by character through this
> text, looking for tags, but that is obviously way too slow.
> 
> Now I have something like:
> 
> preg_replace_callback ("/<(.+)>/", array($this, 'tag_callback'),
> $this->content);
> 
> But then I don't know how to interpret different tags differently. Any
> suggestions?
> 
> (A tag looks like: <CANTR REPLACE NAME=text> where then the whole tag
> has to be replaced by something called 'text' that has to be looked up
> in a table. So, <CANTR REPLACE NAME=text> has to be replaced with
> something else than <CANTR REPLACE NAME=main> - well, you get the
idea.)

Here's an example of how to use preg_replace_callback. Within the
callback() function, $matches[1] is going to contain whatever value was
in your NAME attribute of your CANTR tag. Act accordingly to it.

<?php

$text = 'Hello <cantr replace text="name">. You are on page <cantr
replace text="main"> right now.';

function callback($matches)
{
    switch($matches[1])
    {
        case 'name':
            $retval = 'John';
        break;
        
        case 'main':
            $retval = 'Index.php';
        break;
    }
    
    return $retval;
}

$new_text = preg_replace_callback('/<cantr replace
text="([^"]+)">/i','callback',$text);

echo "<hr>";
echo $new_text;
?>

---John W. Holmes...

PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/



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

Reply via email to