【问题标题】:Storing a piece of the string for later use in replacement存储一段字符串供以后替换使用
【发布时间】:2010-12-07 20:50:22
【问题描述】:

不确定主题是否清晰、难以描述、易于展示:

我需要转换这个:

$text = 'Bunch of text %img_Green %img_Red and some more text here';

到这里:

$text = 'Bunch of text <img src="/assets/images/Green.gif" alt="Green" /> <img src="/assets/images/Red.gif" alt="Red" /> and some more text here';

提前致谢。

【问题讨论】:

  • 抱歉,已修复代码 sn-ps。

标签: php regex search replace


【解决方案1】:

应该是

$text = preg_replace('/%img_(\w+)/', '<img src="/assets/images/\1.jpg" alt="\1"/>', $text);

查看preg_replace doc page 了解其工作原理

【讨论】:

  • 辛苦了。谢谢贾斯汀!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-08
  • 2021-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-04
  • 1970-01-01
相关资源
最近更新 更多