Search code examples
phpfunctionlineeraseplaintext

HTML string remove entire line


I have an string that contains the body of an e-mail on html. So I need to remove one of the body lines. I made a dirty trick that work, but i guess I can found a more efficient way.

For instance I have:

<p style="background:white">
<span style="font-size:10.0pt;font-family:&quot;Trebuchet MS&quot;,&quot;sans-serif&quot;">
   Coût element : 43.19 €. 
   <u></u><u></u>
</span>
</p>

And I want to remove the entire line. I did a function that removes just the price, and stops when it fount the first letter of the next line (in that case "L"). The function:

function clear_french($body){
   $value = utf8_encode(quoted_printable_decode('element'));
   $ini = strpos($body,$value);
   $i = 0;
   if($ini === false){}
   else{
        while ( $body[$ini+strlen($value)+$i] != 'L' ) {
            //echo $body[$ini+strlen($value)+$i];
            $body[$ini+strlen($value)+$i]="";
            $i++;
        }
        if ($body[$ini+strlen($value)+$i] == 'L'){
            $body[$ini+strlen($value)+$i-1]=" ";
        }

}
return $body;
}

But I don't know if there are any efficient way to do that more clean and fast. And I dunno if it should be more easy to work with text plain in $body, if it's easier how can I do it? Note: I want to delete the cost, because I have to resent the e-mails without it.

Hope anyone helps me! Thanks in advance.


Solution

  • I finally did it with regex, with something like taht:

    $body = preg_replace('#(Translation\s+cost)\s*:\s*\d+(\.\d+)?#u', '$1:', $body);
    

    And it works!