Pregunta

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.

¿Fue útil?

Solución 2

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!

Otros consejos

A dirty little trick I use when I want to have placeholders and conditional parts in my HTML emails is using HTML comments. If you have control over the HTML that's sent, you can have your HTML emails set up like this :

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

And then send the email containing the price once. (Price will appear because it is not commented)

Then you can use preg_replace to strip that part :

$newBody = preg_replace('<!-- __CONDITION_NAME_START__ -->(.|\n)*?<!-- __CONDITION_NAME_END__ -->', '', $body);

And send $newBody as your email not containing the price

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top