php—从字符串中查找特定文本并将其与数据库匹配

8oomwypt  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(272)

我有一个网站,人们可以创建配方,在创建配方时,他们可以把配料分为几个步骤,我希望在每个步骤中添加的配料突出显示,并与数据库匹配,这样我就可以改变重量,并与配料细节等链接。
因此,当用户为配料中的步骤编写文本时,它是这样的
在这个步骤中,你必须加入[400克水{65}]和[10克油{61}]煮30分钟。
其中,[400g water{65}]400g是重量water是只显示给用户的配料名称,{65}是数据库中的id,我稍后会匹配它
我现在要做的是
测试

$text="Pour 1 cup of the cream into a saucepan 
and add the sugar, salt. Scrape the seeds of the vanilla bean into the pot and then add the vanilla pod to the pot. Warm the 
mixture over medium heat, just until the sugar dissolves. Remove from the heat and add the remaining cream, milk, and 
vanilla. Stir to combine and chill in the refrigerator.[100g water{64}] [40g oil{61}] ";
    $text2=$text;
    $checkid=substr_count("$text2",'[');
        while ($checkid > 0){
    $pos = strpos($text2, '{');
    $pos2 = strpos($text2, '}');
    $pos3=$pos2-$pos-1;
    $datas=substr($text2,$pos+1,$pos3);

    $datas2=substr($text2,$pos+1,$pos3);
        $getdatas_dats=mysqli_query($conn,"select rising,riswei,inname from recipe_ing_steps r,ingre i where r.risid='$datas' and r.rising=i.inid");
        $thedatass=mysqli_fetch_array($getdatas_dats);
        $string=" <a href='#'>".$thedatass['riswei'].' grams of '.$thedatass['inname']."</a> ";
            $text2=preg_replace("/\[[^]]+\]/","",$text2,1); 
            echo $string;
            $checkid --;
        }

这给了我来自数据库的两种成分的名称,但我想用每个文本中受尊重的字符串替换每个[400g水{65}]。

dtcbnfnu

dtcbnfnu1#

$text="Pour 1 cup of the cream into a saucepan and add the sugar, salt. Scrape the seeds of the vanilla bean into the pot and then add the vanilla pod to the pot. Warm the mixture over medium heat, just until the sugar dissolves. Remove from the heat and add the remaining cream, milk, and vanilla. Stir to combine and chill in the refrigerator.[100g water{64}] [400g water{65}] ";
$text2=$text;
$checkid=substr_count("$text2",'[');
    while ($checkid > 0){
$pos = strpos($text2, '{');
$pos2 = strpos($text2, '}');
$pos3=$pos2-$pos-1;
$datas=substr($text2,$pos+1,$pos3);

    $getdatas_dats=mysqli_query($conn,"select rising,riswei,inname from recipe_ing_steps r,ingre i where r.risid='$datas' and r.rising=i.inid");
    $thedatass=mysqli_fetch_array($getdatas_dats);
    $string=" <a href='#'>".$thedatass['riswei'].' grams of '.$thedatass['inname']."</a> ";
        $text2=preg_replace("/\[[^]]+\]/","$string",$text2,1); 
        $checkid --;
    }
echo $text2;

这个很好用

相关问题