It's certainly possible (though questionable to me as to whether it's worth the effort 🙂 ). You could use preg_replace() with coordinated arrays for the first two arguments:
$search = array(
'/===={([^}]*)}(.*)====/s',
'/====(.*)====/s',
'/==={([^}]*)}(.*)===/s',
'/===(.*)===/s',
'/=={([^}]*)}(.*)==/s',
'/==(.*)==/s',
'/(?<!class)={([^}]*)}(.*)=/s',
'/(?<!class)=(.*)=/s'
);
$replace = array(
'<h4 class="\\1">\\2</h4>',
'<h4>\\1</h4>',
'<h3 class="\\1">\\2</h3>',
'<h3>\\1</h3>',
'<h2 class="\\1">\\2</h2>',
'<h2>\\1</h2>',
'<div class="\\1">\\2</div>',
'<div>\\1</div>'
);
echo(preg_replace($search, $replace, $string));