> On Mon, 06 Aug 2001, [EMAIL PROTECTED] wrote:
> > ID: 12477
> > User updated by: [EMAIL PROTECTED]
> > Reported By: [EMAIL PROTECTED]
> > Status: Closed
> > Bug Type: Feature/Change Request
> > Operating System: Linux and SCO
> > PHP Version: 4.0.6
> > New Comment:
> >
> > I was trying to have to walk the array twice.
> >
> > The following works:
> >
> > $pagevars = str_replace("$","\\\$",$pagevars);
> > $page = preg_replace($template,$pagevars,$page);
> >
> > The reason for using preg_replace is that it does a recursive? replace.
>
> What do you mean, 'recursive' replace?

Example (using shortened arrays):

In my system I build a complete page and then echo it to the browser.  All of the 
Navigation records and other content pages for the
site are contained in a MySql database.
The only fielsystem files that exist are the main index.php3 script, a site 
configuration file that contains defaults for variables
that is imported into a class called "site", and the template pages.
The Navigation record for a page determines what needs to happen to it 
programmatically (form, database form, text, upload, download
etc...) and whether the page comes from HTTPS or HTTP servers.

//SETUP PAGE STUFF
$template=array(
  "/({PAGEMAIN})/",
  "/({PAGELINKS})/",
  "/({PAGETEXT})/",
  "/({LINKSDATA})/",
  "/({PAGEDATA})/"
);

$pagevars=array(
  "PAGEMAIN" => get_page($site->page_main),
  "PAGELINKS" => get_page($site->page_links),
  "PAGETEXT" => get_page($site->page_text),
  "LINKSDATA" => "",
  "PAGEDATA" => ""
);

$page = "{PAGEMAIN}";

$pagevars = str_replace("$","\\\$",$pagevars); // preserve dollar signs in page text
$page = preg_replace($template,$pagevars,$page);

The get_page() fuinction just grabs a text file and stuffs its contents into the 
variable.

PAGEMAIN
--------
<!-- OUTPUT MAIN BODY TABLE -->
<table border='0' cellpadding='0' cellspacing='0' width='{WIDTH}' height='77%'>
  <tr valign='top'>{PAGELINKS}{PAGETEXT}</tr>
</table>
<!-- OUTPUT MAIN BODY TABLE -->

PAGELINKS
---------
<!-- OUTPUT LINKS SECTION CELL -->
<td class='background'>
  <table class='linkstable' width='160' border='0' cellspacing='0' cellpadding='0'>
    {LINKSDATA}
    <tr valign='top'>
      <td class='linksgap'><img src='/{IMAGES}/clear.gif' width='158' height='3' 
border='1'></td>
    </tr>
  </table>
</td>
<!-- OUTPUT LINKS SECTION CELL -->

PAGETEXT
--------
<!-- OUTPUT PAGE TEXT TABLE -->
<td width='{WIDTH}'>
  <table width='99%'>
    <tr valign='top'><td><img src='/{IMAGES}/clear.gif' border='0' height='15' 
width='99%'></td></tr>
    <tr valign='top'>
      <td><!--PAGEDATA-->
{PAGEDATA}
<!--PAGEDATA-->
      </td></tr>
    <tr valign='top'><td><img src='/{IMAGES}/clear.gif' border='0' height='15' 
width='99%'></td></tr>
  </table>
</td>
<!-- OUTPUT PAGE TEXT TABLE -->

Because preg_replace walks through the array from top to bottom it becomes "sort of" 
recursive as the resulting string keeps getting
bigger as each variable replacement occurs.

$page
- starts with "{PAGEMAIN}"
- replaced with PAGEMAIN template which contains "{PAGELINKS}{PAGETEXT}"
- {PAGELINKS} is replaced with PAGELINKS template which contains {LINKSDATA}
- {PAGETEXT} is replace with PAGETEXT template which contains {PAGEDATA}
- {LINKSDATA} is replaced with programmatically built HTML for links on page
- {PAGEDATA} is replaced with programatically built HTML for the text of the page 
(form, document etc..)

The end result is a string of HTML.

I hope this is clear.  I may have put you wrong with the use of the word "recursive"?  
I wasn't really sure what term to use.  "Ever
Expanding"?

Regards

Grant Walters
Brainbench 'Most Valuable Professional' for Unix Admin
P O Box 13-043 Johnsonville, Wellington, NEW ZEALAND
Telephone: +64 4 4765175, CellPhone 025488265, ICQ# 23511989


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to