求过滤alt的正则表达式
求PHP过滤alt的正则表达式,alt有三种情况,要求都能过滤掉
alt="求过滤alt的正则表达式解决办法"
alt='xxxxxxxxxxxx'
alt=xxxxxxxxxxxx
要求能过滤掉以上三种情况的。
示例:
<img src="xxxx" alt="求过滤alt的正则表达式解决办法" >
<img alt="求过滤alt的正则表达式解决办法" src="xxxxxxxx">
<img src="xxxxxx" alt=xxxxxxx >
<img alt=xxxxxxxxxxx src="xxxxxxx">
<img src='xxxx' alt='xxxxxxxxx' >
<img alt='xxxxxxxx' src='xxxxxxxx'>
……
要求能过滤各种情况,又不会过滤其它内容。谢谢。
正则表达式 php
[解决办法]
我知道我那个正则有点问题,加那个 \b 是不得已而为之
不过既然能满足你示例的数据,那也就可以了。
你总不能让我去罗列一起可能出现的情况吧?那要你干什么呢?
$s =<<< TXT<P align=center><img src="/uploads/allimg/c100508/12I33243OF-12228.jpg" border=0>
<P align=center><img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I33243OF-12228.jpg" border=0>
<img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I33243Y40-29608.jpg" border=0>
<img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I33243c60-33252.jpg" border=0>
<img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I332440Y0-410N.jpg" border=0>
<img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I332441P-53227.jpg" border=0><BR><BR>
<img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I332442M0-62002.jpg" border=0>
<img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I332443L0-N248.jpg" border=0>
<img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I332444I0-X239.jpg" border=0>
<img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I332445F-953E.jpg" border=0>
<img src="/uploads/allimg/c100508/12I33244C30-105E1.jpg" border=0 alt=如果QQ我是你></P>
TXT;
echo preg_replace('/alt=([\'"]?).+?\1(\s
[解决办法]
>)/is', '$2', $s);
<img src="/uploads/allimg/c100508/12I33243c60-33252.jpg" border=0>
<img src="/uploads/allimg/c100508/12I332440Y0-410N.jpg" border=0>
<img src="/uploads/allimg/c100508/12I332441P-53227.jpg" border=0><BR><BR>
<img src="/uploads/allimg/c100508/12I332442M0-62002.jpg" border=0>
<img src="/uploads/allimg/c100508/12I332443L0-N248.jpg" border=0>
<img src="/uploads/allimg/c100508/12I332444I0-X239.jpg" border=0>
<img src="/uploads/allimg/c100508/12I332445F-953E.jpg" border=0>
<img src="/uploads/allimg/c100508/12I33244C30-105E1.jpg" border=0 ></P>
[解决办法]
$str=<<< TXT
<P align=center><img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I33243OF-12228.jpg" border=0>
<img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I33243Y40-29608.jpg" border=0>
<img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I33243c60-33252.jpg" border=0>
<img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I332440Y0-410N.jpg" border=0>
<img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I332441P-53227.jpg" border=0><BR><BR>
<img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I332442M0-62002.jpg" border=0>
<img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I332443L0-N248.jpg" border=0>
<img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I332444I0-X239.jpg" border=0>
<img alt=如果QQ我是你 src="/uploads/allimg/c100508/12I332445F-953E.jpg" border=0>
<img src="/uploads/allimg/c100508/12I33244C30-105E1.jpg" border=0 alt=如果QQ我是你></P>
TXT;
echo preg_replace('/(.*?)(alt=["\']?.*?["\']?)(\s+
[解决办法]
>)/', '$1$3', $str);
忘记靠在后面没有空格的情况了,加上允许在最后没有空格的境况就可以了