Removing stray brackets from in between shortcodes
You can simplify and improve this part:


$pattern = '/\[('.$k.'.*?(?=\]))\]/';
$content = preg_replace($pattern,'*{*$1*}*',$content);
$pattern = '/\[\/('.$k.'.*?(?=\]))\]/';
$content = preg_replace($pattern,'*{*/$1*}*',$content);



Instead of using two replacements for [k] and [/k],
you can add the /? inside the capture group and use a single replacement:

$pattern = '/\[(\/?'.$k.'.*?(?=\]))\]/';
$content = preg_replace($pattern,'*{*$1*}*',$content);


This regex can be still improved. Is the positive look-ahead really necessary?
It seems to me that this should do the job just as well:

$pattern = '/\[(\/?'.$k.'.*?)\]/';


On the other hand, this is not very strict. For example,
if you have shortcode gallery, then [galleryzzz] will also match and not be treated as "stray brackets". Maybe that's fine with you, but in any case,
it's easy to prevent that by adding a zero-width word boundary check:

$pattern = '/\[(\/?'.$k.'\b.*?)\]/';