更改的属性值和文本标签

发布于 2024-12-04 06:04:27 字数 899 浏览 1 评论 0原文

在我的论坛上,当上传图像时,它将获得以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧情勿念 2024-12-11 06:04:27

您只需逃避减号:

$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\>',$page);

另请参阅我的演示

=== 更新 ===

$page=preg_replace('/\<a.*?href="attachment.php\?aid=([0-9]+)".*?\>([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);

我的新演示

You only have to escape the minus:

$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\>',$page);

Also see my demo.

=== UPDATE ===

$page=preg_replace('/\<a.*?href="attachment.php\?aid=([0-9]+)".*?\>([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);

My new demo.

嘿咻 2024-12-11 06:04:27

这样就够了吗?

\[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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文