英文:
PHP preg_replace all commas inside brackets and square bracket
问题
$clean_ingredients = preg_replace('/(\([^)]*),([^)]*\)|\[[^]]*),([^]]*\])/u', '$1! $2', $ingredients );
英文:
I'm looking to replace all commas that exist within a bracket or square bracket using preg_replace.
I am able to replace all commas inside brackets using the below:
$clean_ingredients = preg_replace('/(\([^)]*),([^)]*\))/', '$1! $2', $ingredients );
This results with commas inside brackets being replaced but I can't work out how to also check for commas inside square brackets.
What I would like to achieve is:
$ingredients = "Skim Milk Powder, Condensed Milk, Coconut Cream, Strawberry Jam [Cane Sugar, Strawberries (40%), Gelling Agent (Fruit Pectin), Acidity Regulator (330)], Hazelnuts, Barley Malt Extract, Emulsifier (Soya Lecithin), Peppermint [Vegetable Oil, Peppermint Oil, Antioxidant (Mixed Tocopherols Concentrate)], Salt, Citric Acid, Flavouring (Vanillin), Vanilla, Colouring (E102, E133, E129, E132, E171, E122, E124, E110, E172, E153).";
$clean_ingredients = preg_replace('????', '$1! $2', $ingredients );
echo $clean_ingredients;
// Output: Skim Milk Powder, Condensed Milk, Coconut Cream, Strawberry Jam [Cane Sugar! Strawberries (40%)! Gelling Agent (Fruit Pectin)! Acidity Regulator (330)], Hazelnuts, Barley Malt Extract, Emulsifier (Soya Lecithin), Peppermint [Vegetable Oil! Peppermint Oil! Antioxidant (Mixed Tocopherols Concentrate)], Salt, Citric Acid, Flavouring (Vanillin), Vanilla, Colouring (E102! E133! E129! E132! E171! E122! E124! E110! E172! E153).
Is there any regex experts out there that know what could achieve what I'm after?
答案1
得分: 1
你可以使用以下 PHP 代码:
<?php
$ingredients = "aaa (1,2,3) bbb [4,5,6,7]";
$clean_ingredients = preg_replace_callback('/\([^()]*\)|\[[^][]*]/', function($m) {return str_replace(',', '! ', $m[0]);}, $ingredients );
echo $clean_ingredients;
// => aaa (1! 2! 3) bbb [4! 5! 6! 7]
正则表达式\([^()]*\)|\[[^][]*]
匹配:
\([^()]*\)
- 一个(
字符 + 除(
和)
之外的零个或多个字符 + 一个)
字符|
- 或者\[[^][]*]
- 一个[
字符 + 除[
和]
之外的零个或多个字符 + 一个]
字符。
preg_replace_callback
内的函数将所有逗号替换为!
。
英文:
You can use
<?php
$ingredients = "aaa (1,2,3) bbb [4,5,6,7]";
$clean_ingredients = preg_replace_callback('/\([^()]*\)|\[[^][]*]/', function($m) {return str_replace(',', '! ', $m[0]);}, $ingredients );
echo $clean_ingredients;
// => aaa (1! 2! 3) bbb [4! 5! 6! 7]
See the PHP demo and the regex demo.
The \([^()]*\)|\[[^][]*]
regex matches
\([^()]*\)
- a(
char + zero or more chars other than(
and)
+ a)
char|
- or\[[^][]*]
- a[
char + zero or more chars other than[
and]
+ a]
char.
The function inside preg_replace_callback
replaces all commas with !
+ space.
答案2
得分: 1
$ingredients = "脱脂乳粉,炼乳,椰子奶油,草莓果酱 [蔗糖,草莓(40%),果胶(水果果胶),酸度调节剂(330)],榛子,大麦芽提取物,乳化剂(大豆卵磷脂),薄荷 [植物油,薄荷油,抗氧化剂(混合生育酚浓缩物)],盐,柠檬酸,香料(香草醛),香草,色素(E102,E133,E129,E132,E171,E122,E124,E110,E172,E153)。";
echo preg_replace_callback('~[[^]]+|([^)]+~', fn($m) => strtr($m[0], ',', '!'), $ingredients);
英文:
$ingredients = "Skim Milk Powder, Condensed Milk, Coconut Cream, Strawberry Jam [Cane Sugar, Strawberries (40%), Gelling Agent (Fruit Pectin), Acidity Regulator (330)], Hazelnuts, Barley Malt Extract, Emulsifier (Soya Lecithin), Peppermint [Vegetable Oil, Peppermint Oil, Antioxidant (Mixed Tocopherols Concentrate)], Salt, Citric Acid, Flavouring (Vanillin), Vanilla, Colouring (E102, E133, E129, E132, E171, E122, E124, E110, E172, E153).";
echo preg_replace_callback('~\[[^]]+|\([^)]+~', fn($m) => strtr($m[0], ',', '!'), $ingredients);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论