我需要用空字符串替换短代码中的字符串""。这是代码:
$pattern = "#\[(.*)( rep=['\"]{1}1['\"]{1})(.*)\]#i";
$replacement = '[$1$3]';
//Test cases:
$values = [
'This is a [s1 val="1"] test',
'This is a [s1 val="1"] test rep="1"',
'This is a [s1 val="1" rep="1"] test',
'This is a [s1 de="2" rep="1" be="2"] test',
'This is a [rep="1" de="2" ] test',
'This is a [s2 val="1" rep="1" de="2"] test ... This is a [s3 val="1" rep="1" de="2" ] test',
];
foreach($values as $value)
{
echo preg_replace($pattern, $replacement, $value) . "\n";
}
前五个案例按预期工作。不幸的是,第六个案例有两个rep =" 1"里面没有按预期工作,因为我想要rep =" 1"在两个短码中删除。这可能是正则表达式。
注意:rep =" 1"必须在短代码内(在[和]内)才能被替换。