PHP - 如果条件为真,则删除括号内的值

PHP - delete values within brackets if condition true

我只想删除包含 "eur" AND "%" 的括号内的值,但很难开始,因为按 "(" 分解字符串并没有让我走得太远。

示例:

"T-Shirt(grey)(20EUR excl.10% discount)with print" -> "T-Shirt(grey)with print"  

"Jacket(blue)" -> "Jacket(blue)"

我的初步尝试:

$string="T-Shirt(grey)(20EUR excl.10% discount)with print";

$string = explode("(",$string);
foreach($string as $first){
    if(strlen(stristr($first,'eur'))!=0 AND strlen(stristr($first,'%'))!=0){

    }
}   

我在你的代码中没有看到 preg_match:),但这里有一个让你入门的例子:

$e = '#\([0-9]+(EUR).+(\%)(|.+?)\)#';

$lines = [
"T-Shirt(grey)(20EUR excl.10% discount)with print",
"T-Shirt(grey)(20USD excl.10% discount)with print",
"T-Shirt(grey) with no print",
"Some weird dress(black)(100EUR with 0% discount)",
];
foreach ($lines as $line)
{
        if(preg_match($e,$line,$m))
        {
                print $line. " => " . str_replace($m[0],'',$line)."\n";
        }
}

将产生:

T-Shirt(grey)(20EUR excl.10% discount)with print => T-Shirt(grey)with print
Some weird dress(black)(100EUR with 0% discount) => Some weird dress(black)