【发布时间】:2017-10-25 23:11:30
【问题描述】:
假设我们有以下数组:
$regexList = ['/def/', '/ghi/', '/abc/'];
和下面的字符串:
$string = '
{{abc}}
{{def}}
{{ghi}}
';
思路是从上到下遍历字符串,依靠正则表达式列表,找到结果,用匹配出现的模式的大写内容替换无论 regexList array 是什么顺序,都在字符串顺序上。
所以,这就是我想要的输出:
- ABC 匹配:/abc/;
- DEF 匹配:/def/;
- GHI 匹配:/ghi/;
至少
- ABC 匹配模式:2;
- DEF 匹配:0;
- GHI 匹配:1;
这是我正在尝试的代码:
$regexList = ['/def/', '/ghi/', '/abc/'];
$string = '
abc
def
ghi
';
$string = preg_replace_callback($regexList, function($match){
return strtoupper($match[0]);
}, $string);
echo '<pre>';
var_dump($string);
这个输出只是:
string(15) "
ABC
DEF
GHI
"
如何以 $string 顺序(从上到下)获得与这些字符串匹配的偏移量或模式?谢谢。
【问题讨论】:
-
@RST:这与问题无关。
标签: php regex preg-replace-callback