drbz99867 2019-05-26 04:52
浏览 172
已采纳

str_replace函数无法替换多个值[重复]

This question already has an answer here:

I have set of strings and need to replace them using str)replace as we loop through the query as the query is success need to replace each value in PHP.

<?php

include_once("con.php");
$db = new Da();

$con = $db->con();

$lclString = "{{ONE}} {{TWO}} {{THREE}} {{FOUR}} {{FIVE}} {{SIX}}";

 $lclCount = 1;
 if(preg_match_all("/\{\{[^{}]+\}\}/", $lclString, $matches)) {

    foreach ($matches[0] as $match) {

        $lclQuery = "SELECT link, link_value FROM student WHERE link_value = '".$match."'";

        $lclResult = $connString->query($lclQuery);

        while($row = $lclResult->fetch(PDO::FETCH_ASSOC)) {

            $lclNewValue = str_replace($match, $row['link'], $lclString);

            }

        }

        echo json_encode($NewValue);

 } 

?>

Here only second replacement is happening.

</div>
  • 写回答

1条回答 默认 最新

  • dsf5632 2019-05-26 05:01
    关注
    <?php
    
    include_once("con.php");
    $db = new Da();
    
    $con = $db->con();
    
    $lclString = "{{ONE}} {{TWO}} {{THREE}} {{FOUR}} {{FIVE}} {{SIX}}";
    
     $lclCount = 1;
     if(preg_match_all("/\{\{[^{}]+\}\}/", $lclString, $matches)) {
     $lclNewValue = $lclString;
    foreach ($matches[0] as $match) {
    
        $lclQuery = "SELECT link, link_value FROM student WHERE link_value = '".$match."'";
    
        $lclResult = $connString->query($lclQuery);
    
        while($row = $lclResult->fetch(PDO::FETCH_ASSOC)) {
    
            $lclNewValue = str_replace($match, $row['link'], $lclNewValue);
    
            }
    
        }
    
        echo json_encode($NewValue);
    
     } 
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Opencv配置出错
  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?