PHP preg_replace替换括号和方括号内的所有逗号。

huangapple go评论96阅读模式
英文:

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]

请查看PHP演示正则表达式演示

正则表达式\([^()]*\)|\[[^][]*]匹配:

  • \([^()]*\) - 一个(字符 + 除()之外的零个或多个字符 + 一个)字符
  • | - 或者
  • \[[^][]*] - 一个[字符 + 除[]之外的零个或多个字符 + 一个]字符。

preg_replace_callback内的函数将所有逗号替换为!

英文:

You can use

&lt;?php

$ingredients = &quot;aaa (1,2,3) bbb [4,5,6,7]&quot;;
$clean_ingredients = preg_replace_callback(&#39;/\([^()]*\)|\[[^][]*]/&#39;, function($m) {return str_replace(&#39;,&#39;, &#39;! &#39;, $m[0]);}, $ingredients );
echo $clean_ingredients;
// =&gt; 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 = &quot;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).&quot;;

echo preg_replace_callback(&#39;~\[[^]]+|\([^)]+~&#39;, fn($m) =&gt; strtr($m[0], &#39;,&#39;, &#39;!&#39;), $ingredients);

huangapple
  • 本文由 发表于 2023年5月11日 14:37:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定