- var_dump: documentation ( source)
- preg_replace: documentation ( source)
<?php
$reg_exp = "/(^<table.+.(?:[n])?(?:.+)?)(<tbody>)/s";
$replace_with = "/1<thead>";
echo $input = '<table class="table table-bordered table-responsive" style="width: 100%;">
<tbody>';
$final = preg_replace($reg_exp, $replace_with, $input);
var_dump($final);