简体   繁体   English

str_replace php用参数替换文本

[英]str_replace php replacing text with a parameter

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 UPDATE

var_dump($value); 后续代码var_dump($值);

  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" 
   }

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....

Marc B is right. 马克B是对的。 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! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM