如何循环、匹配和替换?

我有多个带有相同花括号的字符串,如果计数为 1,则需要将它们替换为动态字符串,然后需要替换第一次出现,如果计数为 2,则替换第二次出现,依此类推,直到条件满足。


<?php


include_once("con.php");

$db = new Da();


$con = $db->con();


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


 $Count = 1;

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


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

        $Count++;

        $Query = "SELECT link FROM student WHERE linkVal = '".$match."'";

        $Result = $con->query($Query);


        if($row = $Result->fetch(PDO::FETCH_ASSOC)) {


            $NewValue = preg_replace("/\{\{[^{}]+\}\}/", $row["link"], $String);


        }

    }


        echo json_encode($NewValue);


 } 



?>

如果第一次出现,{{ONE}} 应该用 $row["link"] 替换为新值,然后用新值替换 {{TWO}},依此类推。


温温酱
浏览 162回答 3
3回答

慕哥6287543

在每个匹配的循环中preg_replace,我建议您使用str_replace:if(preg_match_all("/\{\{[^{}]+\}\}/", $lclString, $matches)) {&nbsp; &nbsp; $NewValue = $String;&nbsp; &nbsp; foreach ($matches[0] as $match) {&nbsp; &nbsp; &nbsp; &nbsp; $Count++;&nbsp; &nbsp; &nbsp; &nbsp; $Query = "SELECT link FROM student WHERE linkVal = '".$match."'";&nbsp; &nbsp; &nbsp; &nbsp; $Result = $con->query($Query);&nbsp; &nbsp; &nbsp; &nbsp; if($row = $Result->fetch(PDO::FETCH_ASSOC)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $NewValue = str_replace($match, $row["link"], $NewValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^^^^^^^^^^^^^^^^^^&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; echo json_encode($NewValue);}&nbsp;

潇湘沐

您可以通过在一个查询中获取所有替换值来极大地简化代码:$String = "{{ONE}} {{TWO}} {{THREE}} {{FOUR}} {{FIVE}} {{SIX}}";if(preg_match_all("/\{\{[^{}]+\}\}/", $String, $matches)) {&nbsp; &nbsp; $Query = "SELECT linkVal, link FROM student WHERE linkVal IN('".implode("','", $matches[0])."')";&nbsp; &nbsp; $Result = $con->query($Query);&nbsp; &nbsp; if ($rows = $Result->fetchAll(PDO::FETCH_ASSOC)) {&nbsp; &nbsp; &nbsp; &nbsp; $NewValue = str_replace(array_column($rows, 'linkVal'), array_column($rows, 'link'), $String);&nbsp; &nbsp; }&nbsp; &nbsp; echo json_encode($NewValue);}&nbsp;

哆啦的时光机

您的代码存在一些问题,您需要确保 中的变量preg_match_all()是您尝试搜索的字符串。但主要问题在于更换部件。您需要替换当前匹配值 ( $match) 并将其替换为新字符串 - 目前您总是替换原始字符串中的新匹配。在这里,我$NewValue从原始字符串创建并不断替换其中的值...if(preg_match_all("/\{\{[^{}]+\}\}/", $String, $matches)) {&nbsp; &nbsp; $NewValue = $String;&nbsp; &nbsp; foreach ($matches[0] as $match) {&nbsp; &nbsp; &nbsp; &nbsp; $Count++;&nbsp; &nbsp; &nbsp; &nbsp; $Query = "SELECT link FROM student WHERE linkVal = '".$match."'";&nbsp; &nbsp; &nbsp; &nbsp; $Result = $con->query($Query);&nbsp; &nbsp; &nbsp; &nbsp; if($row = $Result->fetch(PDO::FETCH_ASSOC)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $NewValue = preg_replace("/".preg_quote($match)."/",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $row["link"], $NewValue);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; echo json_encode($NewValue);}您还应该考虑使用准备好的语句,因为目前您可能会遇到 SQL 注入问题。
打开App,查看更多内容
随时随地看视频慕课网APP