Strip out a substring beginning with a specific word and ending with "."

84 views Asked by At

From the middle of a text I need to cut out a sentence or better the information about the ingredients of a product. The logic behind is always the same. Starting with "Ingredients" ending with a dot "."

For example (this is my $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

I tried so far with strpros but the fact it is in the middle of the text I get everything from "Ingredients" on until the end.

I need only this as output:

$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."

Given that $prodDesc is the description above, my try was:

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

But as mentioned, I get everything on from "Ingredients" until the end of the description. But it should stop at the first full stop in the example at "Ingredients... ...natural Flavours."

6

There are 6 answers

1
Saeed M. On BEST ANSWER

try with 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];

Output:

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 Flavou rs

0
zeljko miloradovic On

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

0
Robert Price On

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;
1
Indrasis Datta On

You're almost there. $prodIngredientsData stores the string after "Ingredients ". So, we need to extract the string between "Ingredients " and the first "."

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;
}
0
DonCallisto On

You can use preg_replace for this kind of tasks.

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

The regex Ingredients:[^\.]+\. indicates a string (placed basically everywhere in the $prodIngredientsData) matching (literally) Ingredients: and followed by any set of chars but a point [^\.] with at least an occurrence (+) and ending with a point \.

Please pay attention: if ingredients has a point somewhere and continues, this basically will strip off only a certain part of them.

0
LF-DevJourney On

You can search the begin and end with str_pos and save the string between them, then do next search till to end. Check the demo

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

The demo 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)."
}