php正则表达式除什么之外,php - 正则表达式:剥离除SRC之外的HTML属性

博客讨论了如何使用PHP的preg_replace函数编写正则表达式,以保留HTML标签中的'src'属性,同时移除其他所有属性。提供的解决方案详细解释了正则表达式的组成部分,并提醒注意其局限性,可能不适用于所有HTML输入。建议使用更专业的标记/属性过滤器,如Zend_Filter_StripTags。
摘要由CSDN通过智能技术生成

我正在尝试编写一个正则表达式,它将除去除src属性之外的所有标记属性。例如:

This is a paragraph with an image image.jpg

将退回如下:

This is a paragraph with an image image.jpg

我有一个正则表达式来去除所有属性,但是我正在尝试调整它以将其留在SRC中。以下是我目前为止的情况:

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

为此使用php的preg_replace()。

谢谢!

伊恩

最佳答案

这可能适用于您的需求:$text = '

This is a paragraph with an image image.jpg

';

echo preg_replace("/]*(\ssrc=['\"][^'\"]*['\"]))?[^>]*?(\/?)>/i",'', $text);

//

This is a paragraph with an image image.jpg

regexp分解如下:

/ # Start Pattern

< # Match '

( # Start Capture Group $1 - Tag Name

[a-z] # Match 'a' through 'z'

[a-z0-9]* # Match 'a' through 'z' or '0' through '9' zero or more times

) # End Capture Group

(?: # Start Non-Capture Group

[^>]* # Match anything other than '>', Zero or More Times

( # Start Capture Group $2 - ' src="...."'

\s # Match one whitespace

src= # Match 'src='

['"] # Match ' or "

[^'"]* # Match anything other than ' or "

['"] # Match ' or "

) # End Capture Group 2

)? # End Non-Capture Group, match group zero or one time

[^>]*? # Match anything other than '>', Zero or More times, not-greedy (wont eat the /)

(\/?) # Capture Group $3 - '/' if it is there

> # Match '>'

/i # End Pattern - Case Insensitive

添加一些引用,并使用替换文本it should strip any nonsrc=properties from well-formed html tags.

请注意,这不一定适用于所有输入,因为反HTML+regexp的人在下面非常聪明地指出。有一些回退,最显著的是,

将结束

">和其他一些破碎的问题…我建议在PHP中将Zend_Filter_StripTags视为完整的证明标记/属性过滤器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值