如何消除字符串php中的html重复项

we have escalation matrix where we escalate to higher authorities when the case was not closed in particular time.And the email we sent should in particular format where we can change the format later means we are using email templates for that.Whenever the case is not closed we will fetch the case details and based on the email template defined we will send an mail to higher authorities through php mailer code. on completion of while loop for all the cases i am getting the some of the email content same what my result is

$body1='<p>Hi All,</p> <p>Please find the Escalation report  </p> <table style="height:40px;width:100px;" border="1" cellspacing="1" cellpadding="10" align="left"><caption>Escalation Report</caption> <tbody><tr><td>Contact Name</td> <td> Query Topic</td> </tr><tr><td>Maneesh Kotha</td> <td>External</td> </tr></tbody></table><p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p>Thanks and Regards,</p> <p>RF Team.</p> <p>Hi All,</p> <p>Please find the Escalation report  </p> <table style="height:40px;width:100px;" border="1" cellspacing="1" cellpadding="10" align="left"><caption>Escalation Report</caption> <tbody><tr><td>Contact Name</td> <td> Query Topic</td> </tr><tr><td>Sravani Tedla</td> <td>External</td> </tr></tbody></table><p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p>Thanks and Regards,</p> <p>RF Team.</p>';

and i want above text to be modified as is as below

$body1='<p>Hi All,</p> <p>Please find the Escalation report  </p> <table style="height:40px;width:100px;" border="1" cellspacing="1" cellpadding="10" align="left"><caption>Escalation Report</caption> <tbody><tr><td>Contact Name</td> <td> Query Topic</td> </tr><tr><td>Maneesh Kotha</td> <td>External</td> </tr><tr><td>Sravani Tedla</td> <td>External</td> </tr></tbody></table><p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p>Thanks and Regards,</p> <p>RF Team.</p>';

my whole code is `?php

if(!defined('sugarEntry')) define('sugarEntry', true);
require_once('include/entryPoint.php');
global $db;
date_default_timezone_set('UTC');
$date_sms_email= date('Y-m-d H:i:s');
$query_dateentered="select date_entered,id,state_c from cases inner join cases_cstm on cases.id=cases_cstm.id_c where deleted='0' and status='New' and date_of_answer_of_query_c='2015-08-17'";
$result_dateentered=$db->query($query_dateentered);
while($row_dateentered=$db->fetchByAssoc($result_dateentered)){
    $state=$row_dateentered['state_c'];
    $case_id=$row_dateentered['id'];
    $datecreated=$row_dateentered['date_entered'];
    $today_date= date('Y-m-d H:i:s');
    $diff = abs( strtotime($datecreated) - strtotime($today_date));
    $days=intval( $diff / 86400 );
    $hours=intval( ( $diff % 86400 ) / 3600);
    $query_details_level1="SELECT emailtemplate_id_c,sms_text_level1_c,email1_level1_c,email2_level1_c,email3_level1_c,phone_number1_level1_c,phone_number2_level1_c,phone_number3_level1_c FROM bhea_escalation_matrix INNER JOIN bhea_escalation_matrix_cstm ON bhea_escalation_matrix.id=bhea_escalation_matrix_cstm.id_c WHERE state_c='$state' AND deleted='0'";
        $result_details_level1=$db->query($query_details_level1);
        $row_details_level1=$db->fetchByAssoc($result_details_level1);
        $emailtemplateid=$row_details_level1['emailtemplate_id_c'];
        $emailtemplate_query="SELECT body_html,subject FROM email_templates WHERE id='$emailtemplateid'";
        $emailtemplate_result=$db->query($emailtemplate_query);
        $emailtemplate_row=$db->fetchByAssoc($emailtemplate_result);
        $body=$emailtemplate_row['body_html'];
        $subject=$emailtemplate_row['subject'];
        $email1=$row_details_level1['email1_level1_c'];
        $email2=$row_details_level1['email2_level1_c'];
        $email3=$row_details_level1['email3_level1_c'];
        $smstext=$row_details_level1['sms_text_level1_c'];
        $phone1=$row_details_level1['phone_number1_level1_c'];
        $phone2=$row_details_level1['phone_number2_level1_c'];
        $phone3=$row_details_level1['phone_number3_level1_c'];
        $needle = '$acase_';
        $matches = array_filter(str_word_count($body,1,'_$'),
                    function($item) use ($needle) {
                        return (levenshtein($item,$needle,1,1,0) == 0);
                    }
                   );
         // echo "Hai"; 
         foreach ($matches as $values) {
            $correctvariables=$values;
            $queryvariables=str_replace('$acase_',"",$correctvariables);

            $query_casevariables="SELECT $queryvariables FROM cases inner join cases_cstm on cases.id=cases_cstm.id_c where id='$case_id'";
            $result_casevariables=$db->query($query_casevariables);
            $row_casevariables=$db->fetchByAssoc($result_casevariables);
            $result_case=$row_casevariables[$queryvariables];
            $body= str_replace(array($correctvariables),array($result_case),$body);
        }

        $body1.=$body;
        }
        echo $body1;
        ?>

` Thanks in advance

<?php

$body='<p>Hi All,</p> <p>Please find the Escalation report  </p> <table style="height:40px;width:100px;" border="1" cellspacing="1" cellpadding="10" align="left"><caption>Escalation Report</caption> <tbody><tr><td>Contact Name</td> <td> Query Topic</td> </tr><tr><td>Maneesh Kotha</td> <td>External</td> </tr></tbody></table><p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p>Thanks and Regards,</p> <p>RF Team.</p> <p>Hi All,</p> <p>Please find the Escalation report  </p> <table style="height:40px;width:100px;" border="1" cellspacing="1" cellpadding="10" align="left"><caption>Escalation Report</caption> <tbody><tr><td>Contact Name</td> <td> Query Topic</td> </tr><tr><td>Sravani Tedla</td> <td>External</td> </tr></tbody></table><p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p>Thanks and Regards,</p> <p>RF Team.</p>';

$temp=substr($body,0,strpos($body,"<table"));

?>

use this code $temp will contain what you mentioned above have fun