正则表达式:去除除SRC之外的HTML属性 [英] Regex: Strip HTML attributes except SRC

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

问题描述

我试图编写一个正则表达式,它将除去SRC属性以外的所有标签属性。例如:

I'm trying to write a regular expression that will strip 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>');

使用PHP的preg_replace()做这件事。

Using PHP's preg_replace() for this.

谢谢!
Ian

Thanks! Ian

推荐答案

好的,以下是我使用过的程序:

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天全站免登陆