dongzhu7329 2018-12-02 02:00
浏览 65
已采纳

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

I have a website where people can create recipe and while creating recipe they can put ingredients into steps and I want the ingredients added in every step to be highlighted and match with database so that I can change the weight and link it with ingredient details etc.

So when users write text for steps in ingredients it is like this

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

what I am doing right now is

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

This gives me the name of both the ingredient from database but wheat I want is to replace every [400g water{65}] with the respected string in every text .

展开全部

  • 写回答

1条回答 默认 最新

  • dongyuan8024 2018-12-02 03:11
    关注
    $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;
    

    This works fine

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?