【发布时间】:2021-01-26 13:23:19
【问题描述】:
我想用给定字符串中的子字符串“XY”替换模式“binary_function([x,y])”的出现。
我让它使用以下代码:
// $string is the string to be searched
$string = preg_replace_callback('/binary_function\(\[(\S),(\S)\]\)/', function ($word) {
$result = strtoupper($word[1]) . strtoupper($word[2]);
return $result;
}, $string);
但是,我还希望它用子字符串“X1Y1”替换“binary_function([x1,y1])”,以及方括号内的任何长度的参数,例如[x11,y12]、[var1,var2] 等
我试过了:
// $string is the string to be searched
$string = preg_replace_callback('/binary_function\(\[(\S+),(\S+)\]\)/', function ($word) {
$result = strtoupper($word[1]) . strtoupper($word[2]);
return $result;
}, $string);
但它不起作用。
有人可以帮忙吗?
谢谢。
【问题讨论】:
-
'/binary_function\(\[([^][\s,]+),([^][\s,]+)]\)/' -
太棒了 :-) 非常感谢。
标签: php regex preg-replace-callback