Re: [PHP] Template and Replacement

2002-08-13 Thread Bas Jobsen

http://www.php.net/manual/en/function.str-replace.php
In PHP 4.0.5 and later, every parameter to str_replace() can be an array. 

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




[PHP] Template and Replacement

2002-08-13 Thread Alex Shi

Hello,

Currently I am using following method to generate dynamic page
content:

1. Create a page template, in which the places where substitution 
is supposed to take place are marked by a key bracketed with 
'{' and '}'. For example: {news}, {date}, {headlines}, {article_content}..

2. in php script:
- prepare all the content and assign them to varialbes which have 
exactly the same name as the keys in the template. for example:
$news = "...";
$headlines = "...";


- create a array using all the keys in the template. in this example
it is:
$keys = array (news, date, headlines, article_content);

- dump the template into a buffer:
$page = file ($template_file_path);

- do replacement:
foreach ($keys as $key ){
   $page = str_replace ('{'.$key.'}', $$key, $page);
}

- send out the page:
print $page;



OKay, this process works perfect. However I am thinking that 
this might not be a effective way do to replacement because
for each key replacement the whole template has to be scanned.
No, this is too expensive. What I am currently thinking is, just
use one scan but do all replacements. Here is my proposed 
solution:

1. create an associcate array using the keys and their values:
$pattern = array (
  news => "...";
  date => "...",
  headlines => "...",
  
);

2. scan the template and, when found something matched with
'{[what_ever_a_key]}', replace it with $pattern[what_ever_a_key]. 

I would assume that with step 2 just one scan all replacements
can be done. However I don't know with what php function and
regex this solution can be implemented. Any suggestion, advice
are highly appreciated!

alex
-- 
---
TrafficBuilder Network: 
http://www.bestadv.net/index.cfm?ref=7029


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