|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[1999-04-30 20:13 UTC] alex at ibrado dot com
[1999-05-02 12:27 UTC] sas at cvs dot php dot net
[1999-05-11 07:49 UTC] sas at cvs dot php dot net
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Mon Jul 06 15:00:02 2026 UTC |
If the following script seems convoluted/unneccessarily complex, it's because the do_it function is a shortened, combined version of several actual functions. This is adapted from a dynamic block template class I'm working on (similar to FastTemplate). <? function do_it($str,$var,$val) { // $str => string to process // $var => variable to replace // $val => value of the variable ereg("<!-- Start -->(.+)<!-- End -->",$str,$reg); // Get the template block $block=$reg[0]; print "\nBLOCK: [$block]\n"; // Get the stuff between Begin and End $innards=$reg[1]; print "\nINNARDS: [$innards]\n"; // Replace the variable with its value $updated_innards=str_replace("{$var}",$val,$innards); // Using ereg_replace instead of str_replace always works $updated_innards_ereg_replace=ereg_replace("{$var}",$val,$innards); print "\n*********************************\n"; print "*\n* UPDATED_INNARDS: [$updated_innards]\n"; print "*\n* UPDATED_INNARDS_EREG_REPLACE: [$updated_innards_ereg_replace]\n"; print "*\n*********************************\n"; // Replace the template block with the text containing // the evaluated variables $updated_text=str_replace($block,$updated_innards,$str); print "\nUPDATED_TEXT: [$updated_text]\n"; } Header("Content-type: text/plain"); $var="addr"; // {variable} to replace $val="you@address.com"; // value to replace {variable} with // The only difference between $text1 and $text2 is a space // after the second {addr} in $text1 $text1="<UL><!-- Start --><LI>{addr}, that's {addr} <!-- End --></UL>"; $text2="<UL><!-- Start --><LI>{addr}, that's {addr}<!-- End --></UL>"; // Show $text1 with the <!-- Start --> .. <!-- End --> blocks // replaced with "<LI>you@youraddress, that's you@youraddress" print "\n=============================== USING TEXT1 (NO PROBLEM) ======\n"; print do_it($text1,$var,$val); // Try to do the same for $text2 print "\n\n\n=============================== USING TEXT2 ======\n"; print do_it($text2,$var,$val); ?> -- Configured with all optional modules -- php3.ini not changed -- simple substitution using str_replace works: <? $text1="<!-- Begin --><LI>{addr} <!-- End -->"; // with the space $text2="<!-- Begin --><LI>{addr}<!-- End -->"; $text1=str_replace("{addr}","you@address.com",$text1); $text2=str_replace("{addr}","you@address.com",$text2); Header("Content-type: text/plain"); print "TEXT1: [$text1]\n"; print "TEXT2: [$text2]\n"; ?>