正则表达式仅剥离标记

时间:2016-12-12 05:46:59

标签: php regex

$re = '/(<(span|br)\b[^>]*>).*?(<\/\2>)/';
$str = '<p><span>Discover a new tough case for your iPhone 5 with Active Urban</span><span>™</span><span>&nbsp;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>&nbsp;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,我只需要去除标签,而不是其内容。

1 个答案:

答案 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>&nbsp;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>&nbsp;Case will live up to the challenge.</span></p>';
$subst = '';
$result = preg_replace($re, $subst, $str);
echo $result;

Explanation