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

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

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助