gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:36:27 27 4
gpt4 key购买 nike

我有一个网站,人们可以在其中创建食谱,在创建食谱时,他们可以将成分放入步骤中,我希望每个步骤中添加的成分都突出显示并与数据库匹配,以便我可以更改重量并将其与成分链接详细信息等

因此,当用户在成分中编写步骤文本时,就像这样

IN THIS STEP YOU HAVE TO ADD [400g water{65}] and [10g oil{61}] boil it for 30 mins .

where [400g water{65}] 400g is the weight water is the name of ingredient only to show to users and {65} is the id from database with i will match it later

我现在正在做的是

TEST

 $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}] 替换为受尊重的字符串。

最佳答案

$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;

这很好用

关于php - 从字符串中查找特定文本并将其与数据库进行匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53579180/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com