dongzhu7329 2018-12-02 10: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 11: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

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

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法