Solved

STR_REPLACE can you insert php code into the middle part

Posted on 2004-04-15
8
539 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now