preg_replace:在引用后添加数字 [英] preg_replace: add number after backreference
本文介绍了preg_replace:在引用后添加数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
情况
我想使用 preg_replace()
添加一个数字'8'
在每个 [aeiou]
之后。
示例
I want to use preg_replace()
to add a digit '8'
after each of [aeiou]
.
Example
b
$ b
from
abcdefghij
to
a8bcde8fghi8j
问题
我应该如何编写替换字符串? / p>
How should I write the replacement string?
// input string
$in = 'abcdefghij';
// this obviously won't work ----------↓
$out = preg_replace( '/([aeiou])/', '\18', $in);
这是 strong>,所以建议 str_replace()
是不一个有效的答案。
我想知道如何拥有数字
This is just an example, so suggesting str_replace()
is not a valid answer.
I want to know how to have number after backreference in the replacement string.
推荐答案
解决方案是将反向引用包含在 $ {}
。
The solution is to wrap the backreference in ${}
.
$out = preg_replace( '/([aeiou])/', '${1}8', $in);
这将输出 a8bcde8fghi8j
请参阅手册在此特别反向引用的情况。
See the manual on this special case with backreferences.
这篇关于preg_replace:在引用后添加数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文