替换百分比(符号和值)和括号

I receive from the client a string like this one:

"a, b, c, d 5%, e, f, g 76%, h (string,string), i"

I want to create a regExp that can remove from the string 5%, 76% (and any other possible percent value n%) and the brackets (bracket opening is replaced by comma). The desired result is:

"a, b, c, d, e, f, g, h, string, string, i"

Is this possible with PHP?

Yes this is possible with PHP, use this function : http://php.net/manual/en/function.preg-replace.php

You will need to write a regular expression to match your criteria.

You would like to use this code:

preg_replace('/ *\d*\%/','', 'a, b, c, d 5%, e, f, g 76%, h (string,string), i');

Here is example (switch to replace tab and clean the replace text)

$cleaned = preg_replace('/[%()]/', '', $input)

Your definition of the braces is a bit unclear, but if I assume there's no other opening an closing braces, use this:

$line = "a, b, c, d 5%, e, f, g 76%, h (string,string), i";
$line = preg_replace('/\s+\d+%/', '', $line);
$line = preg_replace('/\s*\(/', ', ', $line);
$line = preg_replace('/\s*\)\s*/', '', $line);
$line = preg_replace('/,(\S)/', ', $1', $line);
echo $line;
$string = "a, b, c, d 5%, e, f, g 76%, h (string,string), i";
$string = preg_replace('/\s+\d+%|\)/', '', $string);
$string = str_replace('(', ',', $string);
$string = preg_replace('/\s*,\s*/', ', ', $string);