Solved

STR_REPLACE can you insert php code into the middle part

Posted on 2004-04-15
8
551 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

813 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

18 Experts available now in Live!

Get 1:1 Help Now