Solved

STR_REPLACE can you insert php code into the middle part

Posted on 2004-04-15
8
546 Views
Last Modified: 2007-12-19
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>";
}
0
Comment
Question by:playstat
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:prady_21
ID: 10838624
http://smarty.php.net/

you can try using Smarty and see if it helps
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 10844396
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
0
 

Author Comment

by:playstat
ID: 10845144
could you leave an example for the above please im really new to this thx


0
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 500 total points
ID: 10845383
$mycode = <<<EOD
<?php
  // large quantity of code here
  // you'll need to escape the $ in variables, otherwise they will be parsed
  \$start = \$i;
  \$prev = \$start - 1;
  // etc, etc
?>
EOD;
$head = <<<EOD
<title>My Title</title>
%CODE%
EOD;
$head = str_replace('%CODE%', $mycode, $head);
eval($head);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:playstat
ID: 10846471
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
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 10846733
Did you try it before removing the <?php tags?
0
 

Author Comment

by:playstat
ID: 10847490
yer it keep messing up
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 10952313
Sorry, out of ideas.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now