0

从文本的中间我需要删减一个句子或更好地了解有关产品成分的信息。背后的逻辑总是一样的。以“成分”开头,以点“。”结尾。

例如(这是我的$prodDesc):

Coca Cola is the most famous soft drink in America.
Ingredients: Carbon water, Sugar (sucrose or high-fructose corn syrup (HFCS) depending on country of origin), Caramel colour (E150d), Phosphoric Acid, Caffeine (34 mg/12 fl oz), natural Flavours. Nutrition Facts: 1 Serving Per Container - Serving Size: 1 Can. Total Fat 0g Sodium 45mg Total Carbohydrate 39g Total Sugars (Includes 39g Added Sugars) Cholesterol 0mg Protein 0g Vitamin D 0g Calcium 0g Iron 0g Potassium 0g

到目前为止,我尝试过,strpros但事实上它位于文本的中间,我从“成分”到最后都得到了所有内容。

我只需要这个作为输出:

$prodIngredientsData = "Ingredients: Carbon water, Sugar (sucrose or high-fructose corn syrup (HFCS) depending on country of origin), Caramel colour (E150d), Phosphoric Acid, Caffeine (34 mg/12 fl oz), natural Flavours."

鉴于 $prodDesc 是上面的描述,我的尝试是:

$searchstring = $prodDesc;
$prodIngredientsData = false;
if (strpos($searchstring, "Ingredients") !== false)
{
    $sd_array = explode("Ingredients", $searchstring);
    $sd = end($sd_array);
    $prodIngredientsData = "Ingredients " . $sd;
}
else {
    $prodIngredientsData = false;
}

但如前所述,我从“成分”开始直到描述结束。但它应该在示例中的第一个句号处停止,即“成分... ...天然香料”。

4

6 回答 6

2

尝试使用 preg_match:

$prodIngredientsData = "Ingredients: Carbon water, Sugar (sucrose or high-fructose corn syrup (HFCS) depending on country of origin), Caramel colour (E150d), Phosphoric Acid, Caffeine (34 mg/12 fl oz), natural Flavours."
preg_match('/(Ingredients:([^.]+))/', $prodIngredientsData, $matches);

echo $matches[0];

输出:

成分:碳水、糖(蔗糖或高果糖玉米糖浆 (HFCS),取决于原产国)、焦糖色素 (E150d)、磷酸、咖啡因(34 毫克/12 液量盎司)、天然香料

于 2019-08-21T08:02:16.623 回答
1

您快到了。$prodIngredientsData将字符串存储在"Ingredients"之后。所以,我们需要提取“Ingredients”和第一个“.”之间的字符串。

if (strpos($searchstring, "Ingredients") !== false)
{
    $sd_array = explode("Ingredients", $searchstring);
    $sd = end($sd_array);
    $prodIngredientsData = "Ingredients " . $sd;
    $end_pos   = strpos($prodIngredientsData, ".");
    $prodIngredientsData = substr($prodIngredientsData , 0, $end_pos+1);

} else {
    $prodIngredientsData = false;
}
于 2019-08-21T08:12:37.440 回答
0

You need regex. Something like preg_match('/Ingredients.*?\./', $string, $match);

于 2019-08-21T08:03:00.783 回答
0

You can use strpos again to find the full stop, and shorten the string.

$searchstring = $prodDesc;
$prodIngredientsData = false;
$ingredientsPos = strpos($searchstring, "Ingredients");
if ($ingredientsPos !== false) {
    $prodIngredientsData = substr($searchstring, $ingredientsPos);
    $stopPos = strpos($prodIngredientsData, ".");
    if ($stopPos !== false) {
        $prodIngredientsData = substr(
                    $prodIngredientsData,
                    0,
                    $stopPos + 1);
    }
}
echo $prodIngredientsData;
于 2019-08-21T08:03:03.290 回答
0

您可以搜索开头和结尾str_pos并保存它们之间的字符串,然后进行下一次搜索直到结束。检查演示

$begin_offset = 0;
$result = [];
$string = ""
while(false !== ($begin_offset=strpos($string,"Ingredients",$begin_offset)) && false !== ($end_offset=strpos($string,".",$begin_offset))){
    $result[] = substr($string,$begin_offset,$end_offset-$begin_offset);
    $begin_offset = $end_offset;
}
var_dump($result);

演示结果,

array(2) {
  [0]=>
  string(195) "Ingredients: Carbon water, Sugar (sucrose or high-fructose corn syrup (HFCS) depending on country of origin), Caramel colour (E150d), Phosphoric Acid, Caffeine (34 mg/12 fl oz), natural Flavours."
  [1]=>
  string(77) "Ingredients: Carbon water, Sugar (sucrose or high-fructose corn syrup (HFCS)."
}
于 2019-08-21T09:21:33.893 回答
0

您可以preg_replace用于此类任务。

$strippedString = preg_replace('/Ingredients:[^\.]+\./', '', $prodIngredientsData);

正则表达式表示匹配(字面意思)匹配Ingredients:[^\.]+\.的字符串(基本上放置在$prodIngredientsDataIngredients:[^\.]+\.

请注意:如果成分在某处有一点并继续下去,这基本上只会剥掉其中的一部分。

于 2019-08-21T08:12:46.710 回答