php – preg_replace:在反向引用后添加数字

栏目: PHP · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/18027811/preg-replace-add-number-after-backreference

情况

我想使用preg_replace()在每个[aeiou]之后添加一个数字’8′.

abcdefghij

a8bcde8fghi8j

我该如何写替换字符串?

// input string
$in = 'abcdefghij';

// this obviously won't work ----------↓
$out = preg_replace( '/([aeiou])/', '\18',  $in);

这只是一个示例,因此建议str_replace()不是有效的答案.

我想知道如何在替换字符串中的反向引用后有数字.

解决方案是将反向引用包装在${}中.

$out = preg_replace( '/([aeiou])/', '${1}8',  $in);

这将输出a8bcde8fghi8j

有关此反馈的特殊情况,请参见 the manual .

翻译自:https://stackoverflow.com/questions/18027811/preg-replace-add-number-after-backreference


以上所述就是小编给大家介绍的《php – preg_replace:在反向引用后添加数字》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Art of Computer Programming, Volumes 1-3 Boxed Set

The Art of Computer Programming, Volumes 1-3 Boxed Set

Donald E. Knuth / Addison-Wesley Professional / 1998-10-15 / USD 199.99

This multivolume work is widely recognized as the definitive description of classical computer science. The first three volumes have for decades been an invaluable resource in programming theory and p......一起来看看 《The Art of Computer Programming, Volumes 1-3 Boxed Set》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具