Solved

STR_REPLACE can you insert php code into the middle part

Posted on 2004-04-15
8
563 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP $_POST vs asp request 4 55
Do not understand error message 3 45
Code planning methods/tools? 5 51
Format Date 7 24
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 count occurrences of each item in an array.
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…

738 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