[英]preg_replace multiple strings between brackets (php/regex)
I have string 我有绳子
[test](test\\-test\\-abc) abcde\\-
I would like to have 我想拥有
[test](test-test-abc) abcde\\-
I need to replace \\\\-
with -
with regex/preg_replace. 我需要用regex / preg_replace将
\\\\-
替换为-
。 Need to replace string only between parenthesis. 只需要在括号之间替换字符串。 I found a solution but it only replaces first occurance.
我找到了一个解决方案,但它只能代替第一次出现。
$result = preg_replace("/(\[.*?\])(\(.*?)(\\\\-)(.*?\))/", '$1$2-$4', $str);
You can use preg_replace_callback
: 您可以使用
preg_replace_callback
:
$str = '[test](test\\\\-test\\\\-abc) abcde\\\\-';
$txt = preg_replace_callback('~\([^)]+\)~',
function ($m) { return str_replace('\\\\-', '-', $m[0]); }, $str);
echo $txt;
//=> [test](test-test-abc) abcde\\-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.