2
$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);

The issue with my regex is that it doesn't match self closing tags like br and also it selects the entire span, I just need to strip the tags, not its contents.

eozzy
  • 66,048
  • 104
  • 272
  • 428
  • [`strip_tags`](https://secure.php.net/manual/en/function.strip-tags.php) – Tushar Dec 12 '16 at 05:48
  • [Remove all html tags from php string](//stackoverflow.com/q/14684077) – Tushar Dec 12 '16 at 05:48
  • @Tushar No, don't want to strip all tags, specific tags only. – eozzy Dec 12 '16 at 05:49
  • regular expressions are shit at processing html –  Dec 12 '16 at 05:52
  • @Dagon True, but parsers aren't great either. I'm using HTMLPurifier and it strips a lot of important stuff. I guess you just have to use whatever works when working with crappy malformed HTML. – eozzy Dec 12 '16 at 05:55

1 Answers1

2

You can try this:

<\/?\s*span\s*>|<\s*br\s*\/?\s*>

and replace by empty

$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

Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43