1

I have this function which someone here helped me minimize to be more efficient but the problem is that this more efficient code isn't working the way I had it originally.

New Class Function

public function iterate($d,$fn){
foreach($d as $item=>$value){
  $txt = str_replace('{'.$value.'}',$item[$value],$fn);
  //as well as
  // $txt = str_replace('{$value}',$item[$value],$fn);
  echo $txt;
  }
}

Original Class Function

public function iterate($d,$t,$fn){
   foreach($d as $item){
   if($t == "post"){
    $txt = str_replace('{author}',$item["author"],$fn);
    $txt = str_replace('{id}',$item["id"],$txt );
$txt = str_replace('{content}',$item["content"],$txt);
    $txt = str_replace('{date}',$item["date"],$txt);
    echo $txt;
    }
   }
}

to Instantiate the function I do

easyCMS->iterate($post,'<div class="post" id="post_{id}">{content}</div><div class="author">{author} on {date}</div>');

The new function outputs this:

<div class="post" id="post_{id}">{content}</div>
<div class="author">{author} on {date}</div>

My original function outputs correctly for example.

<div class="post" id="post_1">Hello World</div>
<div class="author">Mr.EasyBB on 5/28/2014</div>

Am I missing something why would my original work perfectly and not this new smaller more "efficient" code not do the trick?

UPDATE

var_dump($value);

  array(12) { 
    [0]=> string(1) "1" ["id"]=> string(1) "1"
    [1]=> string(59) "Hello this is post one testing iterator easyCMS->iterate();" ["content"]=> string(59) "Hello this is post one testing iterator easyCMS->iterate();" 
    [2]=> string(9) "Mr.EasyBB" ["author"]=> string(9) "Mr.EasyBB" 
    [3]=> string(10) "2014-05-24" ["date"]=> string(10) "2014-05-24" 
    [4]=> string(11) "Recent Post" ["category"]=> string(11) "Recent Post" 
    [5]=> string(26) "html,css,javascript,jquery" ["tags"]=> string(26) "html,css,javascript,jquery" 
   }
3
  • Change $item[$value] to this $value[$item] in your new class function. Commented May 28, 2014 at 19:18
  • still same output see minjs.site88.net Commented May 28, 2014 at 19:20
  • must iterate $value $txt = $fn; foreach ($value as $k => $v){$txt = str_replace('{'.$k.'}', $v, $txt);} Commented May 28, 2014 at 19:30

2 Answers 2

2

You're doing the replacement wrong in the function:

public function iterate($d,$fn){
                           ^^^---original string
foreach($d as $item=>$value){
  $txt = str_replace('{'.$value.'}',$item[$value],$fn);
                                                  ^^^---always use the original string

It should be

foreach(...) {
    $fn = str_replace(....., $fn);
    ^^^^---change this
}
echo $fn;

so that you're constantly modifying the string that was modified in the previous iteration.

Right now you're doing

$original (change {foo}) -> $modified
$original (change {bar}) -> $modified

instead of

$original (change{foo}) -> $modified
$modified (change{bar}) -> $modified_again
$modified_again (etc....
Sign up to request clarification or add additional context in comments.

4 Comments

Ok I see what you've done, except that it still did the same thing.
@RahilWazir done both and it still outputs the same. It's odd I can see what Marc B is saying and it's perfectly true, because I had to change my original functions replacee
you also have to move the echo. You're doing it INSIDE the loop, so you'd be constantly dumping out the partially changed string, instead of just the final product after the replacements are done.
See that's what I thought I just never tried it because I didn't want some funky output worse than it already is. I will try that as well Marc--edit-- Moving echo outside of the loop echos only one post the original will echo all post.
0

Marc B is right. Here's what I think you need:

public function iterate($d, $fn) {
    foreach ($d as $item => $value) {
        $fn = str_replace('{' . $item . '}', $value, $fn);
    }
    echo $fn;
}

Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.