doujiurong7210
2017-03-11 06:57
浏览 190
已采纳

如何在php中使用str_replace替换所有数组值的字符串?

        <?php
        $a=array('1'=>'Jan', '2'=>'Feb','3'=>'Mar');
        print_r($a);
        $str ="<p>Due Month</p>";
        echo str_replace("Month","(Jan+Feb+Mar)","Due Month");
        ?>

Desire Output: Due (Jan+Feb+Mar)

How can I get this output from array using str_replace function?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douliang1369 2017-03-11 07:41
    已采纳

    You can try it for your desired output. It works 100%

    $a = array('1'=>'Jan', '2'=>'Feb','3'=>'Mar');
    print_r($a);
    $comma_separated = implode("+", $a);
    $str ="<p>Due Month</p>";
    echo str_replace("Month","$comma_separated","Due Month");
    
    点赞 评论
  • dongpu42006096 2017-03-11 07:12

    Do you need like this?

    $a = array('1'=>'Jan', '2'=>'Feb','3'=>'Mar');
    $replaceStr = "";
    foreach ($a as $months) {
        $replaceStr[] = str_replace($months, "(Jan+Feb+Mar)", $months);
    }
    print_r($replaceStr);
    
    点赞 评论

相关推荐 更多相似问题