Link to home
Start Free TrialLog in
Avatar of playstat
playstat

asked on

STR_REPLACE can you insert php code into the middle part

Hi there

Is there anyway to insert some php code into a str_replace

$head=str_replace('%CODE%,<-------php code-------->,$head);

as at the moment i have the code below but for some reason the templates output it as text and not as the generated php code

I use a HTML template with tags which then converts to php output

As it is the only thing i get are the generated results below on the page display render. Shows all pages generated. I need to insert that into the template for output. Ive tried ob_start(); etc then using a str_replace to insert it into the template but then the $start=$i; dont work. ONly solution i can think of that might work is teh above to actually replace %CODE% with the segment below.

$start=$i;

$prev = $start-1;
$range = $start-3;
$next = $start+1;

if($start > 1) {
   echo "<a href=\"$name-1.php\">First</a> <a href=\"$name-$prev.php\">Prev</a> ";
   if($start > 3){
         echo "[<a href=\"$name-$range.php\">$range</a>] ";
     }
     $range += 1;
     if($start > 2){
          echo "[<a href=\"$name-$range.php\">$range</a>] ";
     }
     echo "[<a href=\"$name-$prev.php\">$prev</a>] ";
}
echo "<strong>[$start]</strong> ";
if($start < $nindexes) {

best Regards

dc
    echo "[<a href=\"$name-$next.php\">$next</a>] ";
    $range = $next+1;
   if($start < ($nindexes-2)){
          echo "[<a href=\"$name-$range.php\">$range</a>] ";
     }
     $range += 1;
     if($start < ($nindexes-3)){
          echo "[<a href=\"$name-$range.php\">$range</a>] ";
     }
     echo "<a href=\"$name-$next.php\">Next</a> <a href=\"$name-$nindexes.php\">Last</a>";
}
Avatar of prady_21
prady_21

http://smarty.php.net/

you can try using Smarty and see if it helps
So is your question really how to execute the contents of a variable as PHP?  Use the eval() function.
http://www.php.net/manual/en/function.eval.php
Avatar of playstat

ASKER

could you leave an example for the above please im really new to this thx


ASKER CERTIFIED SOLUTION
Avatar of snoyes_jw
snoyes_jw
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
its sort of working still ends up printing the code the HREFS are ok though just the rest dont come out I had to remove the <?php ?> as the rest of the script has that already. It left the page output messed up all I get is the body part.

thats what it outputs

start=$i; echo "page $start of $nindexes
"; $prev = $start-1; $range = $start-3; $next = $start+1; if($start > 1) { echo "First Prev "; if($start > 3){ echo "[3] "; } $range += 1; if($start > 2){ echo "[3] "; } echo "[-1] "; } echo "[$start] "; if($start < $nindexes) { echo "[1] "; $range = $next+1; if($start < ($nindexes-2)){ echo "[3] "; } $range += 1; if($start < ($nindexes-3)){ echo "[3] "; } echo "Next Last"; }

I did trry this it works fine for displaying and executing the script

but the increment will not work before it even if its within the pagenumbers script Its as if it executes it once but does not increment the next time it loops.

heres the code

for ($i=1; $i<=$nindexes; $i++)

}

<<<=--- more codee here -->>>>>

ob_start();
include('indexnumbering.php'); -------- code in previous post
$links=ob_get_contents();
ob_end_clean();

$indexheader=str_replace('%PAGE%',$links,$indexheader);

fwrite($fpindex,stripslashes($temp)); /// this part writes to output page from the explode array from the html templates read using these tags below

$temp = explode('<!--BEGIN-->',$index);
             $indexheader = $temp[0];
             $temp = explode('<!--END-->',$temp[1]);
             $indexbody = $temp[0];
      $indexfoot = $temp[1];

Is there anyway to make that increment correctly all it picks up is $i = 1    if it were to count up this script would be completed.
Not sure on the limits of ob functions

Best regards

dc
Did you try it before removing the <?php tags?
yer it keep messing up
Sorry, out of ideas.