dougong2306 2015-11-24 11:50
浏览 26
已采纳

PHP取代每2秒

<?php
        $fact_BB = array("[start]", "[mid]", "[end]");
        $fact_HTML   = array("<tr><td class='FactsTableTDOne'><p>", "</p></td><td class='FactsTableTDTwo'><p>", "</p></td></tr>");

        $str_Facts = str_replace($fact_BB, $fact_HTML, $row['facts']);

        echo $str_Facts;
    ?>

Is it possible to switch between 2 $fact_HTML?

1. $fact_HTMLone = "code";
2. $fact_HTMLtwo = "code";
3. $fact_HTMLone = "code";
4. $fact_HTMLtwo = "code";
5. $fact_HTMLone = "code";

etc. etc.

  • 写回答

2条回答 默认 最新

  • dragonmeng2002 2015-11-24 11:58
    关注

    What you want to achieve is a replace of some strings. I'd suggest a solution like this:

    <?php
    $str_Facts = $row['facts'];
    $replacements = array( "[start]" => "<tr><td class='FactsTableTDOne'><p>",
                           "[mid]"   => "</p></td><td class='FactsTableTDTwo'><p>",
                           "[end]"   => "</p></td></tr>" );
    foreach ($replacements as $repkey => $repval) {
        $str_Facts = str_replace($repkey,$repval,$str_Facts);
    }
    echo $str_Facts;
    ?>
    

    If you want to go on with your approach, you'd loop through the arrays (you have to ensure that the both arrays have the same number of elements).

    <?php
    $str_Facts = $row['facts'];
    for ($i=0;$i<count($fact_BB);$i++) {
        //if you want to switch every uneven, do this:
        if ($i%2!=0) continue;
        $str_Facts = str_replace($fact_BB[$i],$fact_HTML[$i],$str_Facts);
    }
    echo $str_Facts;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度