从 URL 创建 Img 标签
我想要什么
如果字符串中的 URL 在 URL 末尾(不是字符串)包含 .jpg
,那么它应该生成一个使用 preg_replace
从中获取图像,否则创建一个正常链接。
例如:
如果我有 http://www.example.com/images/photo.jpg
那么它应该替换为:
问题:
URL 被替换为任何方式的链接,我的正则表达式都不起作用:(。
我尝试过的:
$content = preg_replace("/(http:\/\/[^\s]+(?=\.jpg))/i","<img src=\"$1\" alt = \"$1\"></img>",$content);
$content = nl2br(preg_replace("/(http:\/\/[^\s]+(?!\.jpg))/m", "<a href=\"$1\" rel=\"nofollow\" target=\"blank\" title=\"$1\" class=\"news-link\">$1</a>", $content));
What I want
If the URL in the string contains a .jpg
at the end of the URL (not the string) then it should make an image from it with preg_replace
else make a normal link.
so for example:
If I have http://www.example.com/images/photo.jpg
then it should replace with:
<img src="http://www.example.com/images/photo.jpg" alt="http://www.example.com/images/photo.jpg">
The problem:
The URL is replaced with a link in any way and my regex isn't working :( .
What I have tried:
$content = preg_replace("/(http:\/\/[^\s]+(?=\.jpg))/i","<img src=\"$1\" alt = \"$1\"></img>",$content);
$content = nl2br(preg_replace("/(http:\/\/[^\s]+(?!\.jpg))/m", "<a href=\"$1\" rel=\"nofollow\" target=\"blank\" title=\"$1\" class=\"news-link\">$1</a>", $content));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个
Try this
你不需要东张西望。就跟着去
You don't need lookaround. Just go with
我认为当您想要向后查找时,您使用了向前查找运算符。您可以将
(?=\.jpg)
更改为(?<=\.jpg)
但还有其他更干净的正则表达式,我相信其他人会发布。I think you used the lookahead operator when you wanted lookbehind. You could change
(?=\.jpg)
to(?<=\.jpg)
but there are other, cleaner regex's I'm sure others will post.这对我有用。
在上图中,路由器 R1 有两个点到点。 ';
苏亚什
This worked for me.
In the figure above, router R1 has two point-to-point . ';
Suyash