更改的属性值和文本标签
在我的论坛上,当上传图像时,它将获得以下 html:
<a target="_blank" href="attachment.php?aid=1" rel="nofollow">zebra.bmp</a>
为了使用我安装的插件,我需要匹配这些类型的 html 并将它们更改为:
<a href="attachment.php?aid=1" rel="fancyzoom">zebra.bmp</a>
在插件上它已经做了一些转换,例如:
$page=preg_replace('/\<a rel="nofollow" href="attachment.php\?aid=([0-9]+)" target="_blank"\>\<img/Usi','<a href="attachment.php?aid=$1" rel="fancyzoom"><img',$page);
现在我试图捕获我在问题开头发布的 html
$page=preg_replace('/\<a target="_blank" href="attachment.php\?aid=([0-9]+)" rel="nofollow"\>([a-zA-Z0-9_- ]+)\.(jpg|jpeg|gif|png|bmp|tif|tiff)\<\/a\>/Usi','<a href="attachment.php?aid=$1" rel="fancyzoom">$2.$3</a>',$page);
但上面的内容似乎不起作用,是因为 OR 括号吗? 有没有办法在我只想要以上述图像扩展名结尾的文件的情况下进行此类替换?
On my forum when an image is uploaded it will gain the follow html:
<a target="_blank" href="attachment.php?aid=1" rel="nofollow">zebra.bmp</a>
In order to use a plugin I have installed, I need to match those kind of html and change them to:
<a href="attachment.php?aid=1" rel="fancyzoom">zebra.bmp</a>
On the plugin it already does some conversion, example:
$page=preg_replace('/\<a rel="nofollow" href="attachment.php\?aid=([0-9]+)" target="_blank"\>\<img/Usi','<a href="attachment.php?aid=$1" rel="fancyzoom"><img',$page);
Now I am trying to catch the html I posted in the begin of my question
$page=preg_replace('/\<a target="_blank" href="attachment.php\?aid=([0-9]+)" rel="nofollow"\>([a-zA-Z0-9_- ]+)\.(jpg|jpeg|gif|png|bmp|tif|tiff)\<\/a\>/Usi','<a href="attachment.php?aid=$1" rel="fancyzoom">$2.$3</a>',$page);
But the above does not seem to work is it because of the OR parenthesis ?
Is there a way to make such replace where I just want the files ending with the above image extensions ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需逃避减号:
另请参阅我的演示。
=== 更新 ===
我的新演示。
You only have to escape the minus:
Also see my demo.
=== UPDATE ===
My new demo.
这样就够了吗?
\[a-zA-Z0-9_-]* .(jpg|jpeg|gif|png|bmp|tif|tiff)\<\/a\>
+ 是此语法中的问题
would this suffice?
\<a target="_blank" href="attachment.php\?aid=([0-9]+)" rel="nofollow"\>[a-zA-Z0-9_-]*.(jpg|jpeg|gif|png|bmp|tif|tiff)\<\/a\>
the + was the problem in this syntax