Re: AGAIN: [PHP] extracting words from templates
| From: | Dean Hall | Date: | Tue, 22 Aug 2000 19:35:47 +0000 |
| Subject: | Re: AGAIN: [PHP] extracting words from templates | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-13006@lists.php.net to get a copy of this message | ||
On Tue, 22 Aug 2000, Jim Brown, Sr. wrote:
> Greetings,
>
> I've got a question:
>
> What's the best way to extract a word from inside a template variable? If the
> variable in the template is '%%TITLE%%', how could I just extract 'TITLE'
> from
> it so I can make it '$TITLE' in my script which will print the title when I call
> 'print "<TITLE>$TITLE</TITLE>";'??
I'm sure there's a simple way to loop through the file with regex, but
I'll make a suggestion based on what I do.
Instead of: <TITLE>%%TITLE%%</TITLE>
Do: <TITLE><?=$TITLE?></TITLE>
<?=$var?> is shorthand for <? echo $var; ?>
It's short and easy; much easier for implementing templates in PHP.
And if you already have implemented it this way, just take my suggestion
with a grain of salt, and wait for the regex guys to answer.
Dean.
>
> Here's what I'm doing now:
>
> $header = file('includes/header.inc');
> while (list(,$val) = each($header)) {
> if (eregi('%%TITLE%%', "$val")) {
> $title = str_replace('%%TITLE%%', "$TITLE", "$val");
> echo "$title";
> }
> elseif (eregi('%%BGCOLOR%%', "$val")) {
> $body = str_replace('%%BGCOLOR%%', "$BGCOLOR", "$val");
> echo "$body";
> }
> else {
> echo "$val";
> }
> }
>
>
> TIA
>
>