$re = '/(<(span|br)\b[^>]*>).*?(<\/\2>)/';
$str = '<p><span>Discover a new tough case for your iPhone 5 with Active Urban</span><span>™</span><span> Case from Cat phones.<br/>Made to survive the challenges of everyday life, this protective case will shield your device whether you\'ve chucked it in your bag, dropped it on the floor or left it in clumsy hands.<br>No matter the situation, the Active Urban</span><span>™</span><span> Case will live up to the challenge.</span></p>';
preg_match_all($re, $str, $matches);
// Print the entire match result
print_r($matches);
我的正则表达式的问题在于它与br
之类的自闭项标签不匹配,而且它还会选择整个span
,我只需要去除标签,而不是其内容。
答案 0 :(得分:2)
你可以试试这个:
<\/?\s*span\s*>|<\s*br\s*\/?\s*>
并替换为空
$re = '/<\/?\s*span\s*>|<\s*br\s*\/?\s*>/m';
$str = '<p><span>Discover a new tough case for your iPhone 5 with Active Urban</span><span>™</span><span> Case from Cat phones.<br/>Made to survive the challenges of everyday life, this protective case will shield your device whether you\\\'ve chucked it in your bag, dropped it on the floor or left it in clumsy hands.<br>No matter the situation, the Active Urban</span><span>™</span><span> Case will live up to the challenge.</span></p>';
$subst = '';
$result = preg_replace($re, $subst, $str);
echo $result;