将两个管道之间的占位符替换为 HTML 标记并使用占位符的值作为 src 值
我无法让这个正则表达式用 标签替换管道包裹的占位符:
$string = 'blah blah |one|';
$search = array('/|one|/','/|two|/');
$string = preg_replace($search,"'<img src=\"\"'.str_replace('|','','\\0').'\".png\"/>'",$string);
我需要它返回 blah blah
在这种情况下,但在处理替换内部的函数时遇到问题。
I can't get this regex to replace pipe-wrapped placeholders with <img>
tags:
$string = 'blah blah |one|';
$search = array('/|one|/','/|two|/');
$string = preg_replace($search,"'<img src=\"\"'.str_replace('|','','\\0').'\".png\"/>'",$string);
I need it to return blah blah <img src="one.png"/>
in this case, but having trouble dealing with the function inside the replacement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个“函数调用”(只是一个字符串)有什么原因吗? 捕获组怎么样?而且您必须转义
|
,因为它们是特殊字符 (alternation< /a>):演示
Is there any reason for this "function call" (which is just a string)? How about capture groups? And you have to escape the
|
, because they are special characters (alternation):DEMO
您可以使用括号捕获不带
|
的名称:/\|(one)\|/
然后,您可以使用
$1
(而不是$0
)引用捕获的名称。You can capture the name without the
|
s using parenthesis:/\|(one)\|/
Then you can reference the captured name using
$1
(instead of$0
).如果您想要将文本放入 || 中,这应该可以工作作为源,并且还可能有多个图像要在同一字符串上转换:
This should work if what you want is put the text inside || as the source and also may have more than one image to convert on the same string:
无需使用数组替换和内联函数使其变得过于复杂。
只需捕获管道之间的值并直接使用即可。
No need to over complicate it with the array replacement and inline function.
Simply capture the value between the pipes and use it directly.