英文:
php how to remove percentage value inside bracket
问题
`如何删除括号内的百分比数值。
示例:
$raw = "orange (43%), blue (2.1) (55%), red yellow (77%), red-blue (80%)";
$remove = preg_replace('/[%]/', '', $raw);
echo $remove;
结果:
orange (43), blue (2.1) (55), red yellow (77), red-blue (80)
我需要的是这样的:
orange, blue (2.1), red yellow, red-blue
英文:
How to remove the percentage value inside the bracket.
example:
<?php
$raw = "orange (43%), blue (2.1) (55%), red yellow (77%), red-blue (80%)";
$remove = preg_replace('/[%]/', '', $raw);
echo $remove;
?>
results:
orange (43), blue (2.1) (55), red yellow (77), red-blue (80)
What I need is like this:
orange, blue (2.1), red yellow, red-blue
答案1
得分: 2
你可以使用\s+(\d+(?:.\d+)?%),然后用""
替换它。请参考regex101.com上的演示。
英文:
You may use
\s+\(\d+(?:\.\d+)?%\)
And replace this with ""
. See a demo on regex101.com.
答案2
得分: 0
这里是代码,100%工作:
$raw = "orange (43%), blue (2.1) (55%), red yellow (77%), red-blue (80%)";
$raw = explode(", ", $raw);
for($a = 0; $a <= count($raw) - 1; $a++){
$test = explode(" ", $raw[$a]);
for($b = 0; $b <= count($test) - 1; $b++){
if(str_contains($test[$b], '%')){
$test[$b] = '';
}
}
$result[$a] = implode(" ", $test);
}
$raw = implode(", ", $result);
echo $raw;
英文:
here you go, works 100%
$raw = "orange (43%), blue (2.1) (55%), red yellow (77%), red-blue (80%)";
$raw = explode(", ", $raw);
for($a = 0; $a <= count($raw) - 1; $a++){
$test = explode(" ", $raw[$a]);
for($b = 0; $b <= count($test) - 1; $b++){
if(str_contains($test[$b], '%')){
$test[$b] = '';
}
}
$result[$a] = implode(" ", $test);
}
$raw = implode(", ", $result);
echo $raw;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论