Home >Backend Development >PHP Tutorial >来问正则表达式了!解决方案

来问正则表达式了!解决方案

WBOY
WBOYOriginal
2016-06-13 10:01:29809browse

来问正则表达式了!!

testtesttes


想通过preg_replace函数把table里的属性只留其中的两项rowspan=6   和colspan=4,此函数里的正则该怎么写??


------解决方案--------------------
$str = '
testtesttes
';
$str = preg_replace( '/\ ].*)?rowspan=[\ "\ ']?([0-9]+)[\ "\ ']?(\s.*)?colspan=[\ '\ "]?([0-9]+)[\ '\ "]?([^\> ].*?)?\> /i ', '
',$str);
echo htmlspecialchars($str);
------解决方案--------------------
:P
$str = '
testtesttes
';
$str = preg_replace( '/\ ].*)?(rowspan|colspan)=[\ "\ ']?([0-9]+)[\ "\ ']?(\s.*)?(colspan|rowspan)=[\ '\ "]?([0-9]+)[\ '\ "]?([^\> ].*?)?\> /i ', '
',$str);
echo htmlspecialchars($str);
------解决方案--------------------
我也贴一个,本贴考虑到的可确保的情况有
1、避免出现引号错误,如:rowspan= "2 '等;
2、避免等号前后出现空格、换行等,如rowspan =
"2 "。

$str = preg_replace( '/\
]+(rowspan|colspan)[\s\r\n]*=[\s\r\n]*([\ "\ ']?)(\d+)\2[^\> ]+(colspan|rowspan)[\s\r\n]*=[\s\r\n]*([\ '\ "]?)(\d+)\5[^\> ]*\> /i ', '
',$str);
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn