Skip to content
Advertisement

Strip out a substring beginning with a specific word and ending with “.”

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):

JavaScript

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:

JavaScript

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

JavaScript

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.”

Advertisement

Answer

try with preg_match:

JavaScript

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

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement