php:动态 preg_replace
下面的代码不是一个功能方法,它只是为了帮助您理解我想要做什么而编写的。
// $i = occurrence to replace
// $r = content to replace
private function inject($i, $r) {
// regex matches anything in the format {value|:value}
$output = preg_replace('/\{(.*?)\|\:(.*?)\}/', '$r', $this->source);
$output[$i]
}
如何在 $output; 中找到 $i 出现的地方?并将其替换为 $r;?
注意:我想做的就是使用 $i (这是一个数字)来查找 preg_replace 中该数字的出现;例如:我可能想用变量 $r 替换第二次出现的 preg_replace 模式
The code below is not a functioning method it's just written to help you understand what I'm trying to do.
// $i = occurrence to replace
// $r = content to replace
private function inject($i, $r) {
// regex matches anything in the format {value|:value}
$output = preg_replace('/\{(.*?)\|\:(.*?)\}/', '$r', $this->source);
$output[$i]
}
How do I find the $i occurrence in $output; and replace it with $r;?
Note: All I want to do is use $i (which is a number) to find the occurrence of that nmber in a preg_replace; For exmaple: I might want to replace the second occurrence of the preg_replace pattern with the variable $r
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你只能通过回调来完成这样的出现计数:
它会按原样保留第一个
$i
匹配项,并在以下情况下使用临时$this->r
:倒计时已匹配。但是可以通过闭包来避免->$i
和->$r
。I think you can only accomplish such an occurence counting with a callback:
It leaves the first
$i
matches as is, and uses the tempoary$this->r
once when the countdown is matched. Could be done with a closure to avoid->$i
and->$r
however.