剥离除src之外的所有HTML属性 [英] Strip all HTML attributes except for src

查看:57
本文介绍了剥离除src之外的所有HTML属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除除 src 属性之外的所有标记属性.例如:

I'm trying to remove all tag attributes except for the src attribute. For example:

<p id="paragraph" class="green">This is a paragraph with an image <img src="/path/to/image.jpg" width="50" height="75"/></p>

将返回为:

<p>This is a paragraph with an image <img src="/path/to/image.jpg" /></p>

我有一个删除所有属性的正则表达式,但是我试图对其进行调整以保留在 src 中.这是我到目前为止的内容:

I have a regular expression to strip all attributes, but I'm trying to tweak it to leave in src. Here's what I have so far:

<?php preg_replace('/<([A-Z][A-Z0-9]*)(\b[^>]*)>/i', '<$1>', '<html><goes><here>');

推荐答案

好的,这是我使用的似乎效果良好的东西:

Alright, here's what I used that seems to be working well:

<([A-Z][A-Z0-9]*)(\b[^>src]*)(src\=[\'|"|\s]?[^\'][^"][^\s]*[\'|"|\s]?)?(\b[^>]*)>

可以随意戳孔.

这篇关于剥离除src之外的所有HTML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆