Search code examples
phphtmlgmailimap

Remove 
 char from imap mail extract


I am trying to fetch a mail in imap from Google, I'm using the imap() function from PHP IMAP library and I want to use a preg_match() call on my mail content but I have a strange issue, I have curious break lines altering the normal preg_match().

More details: I have in my markup something like that:

<TABLE CELLPADDING=5 RULES=GROUPS FRAME=BOX>

<THEAD>
<TR> <TH>Résumé points de classement</TH>       <TH>Extérieur</TH>            <TH>Domicile</TH> </TR>
</THEAD>

<TBODY>
<TR> <TD>Équipe</TD>                      <TD>Milan</TD>         <TD>Arsenal</TD> </TR>

<TR> <TD>Performance du match</TD>         <TD>0</TD>  <TD>19</TD> </TR>
<TR> <TD>Étoiles équipe</TD>         <TD>0</TD>   <TD>0</TD> </TR>
<TR> <TD>Points totaux</TD>              <TD>3195</TD>       <TD>3273</TD> </TR>

<TR> <TD>Niveau actuel</TD>       <TD>22</TD>   <TD>22</TD> </TR>
<TR> <TD>Points pour le prochain niveau</TD> <TD>5</TD>    <TD>127</TD> </TR>
</TBODY>

</TABLE>

I am running this code to extract the body for example:

<?php
// $message is the previous markup.
$str = substr($message, 321, 10);
var_dump($str);
$str = preg_replace("/&#10;/i","",$str);
var_dump($str);
?>

The output of this code is:

<pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>'
&#10;
&#10;&amp;lt;TABLE'</font> <i>(length=13)</i>
</pre>
<pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>'
&#10;
&#10;&amp;lt;TABLE'</font> <i>(length=13)</i>
</pre>

And the extracted value is an empty array... After further investigation i discovered through a var_dump() of my source string that there is a at the end of each line.
I have no clue of what this html char code is and how to remove it.


Solution

  • Well the first thing is I'm assuming that you have run imap_mime_header_decode already and you have the result.

    Assuming you're working in the UTF8 charset and not ISO-8859-1 or ISO-8859-15 charsets you can try

    utf8_encode($string);
    

    EDIT Whoops... to remove or replace the char you can use

    preg_replace('/&#10/','<alt>',$string);
    

    Just place your alternative character in where is. You can replace it with nothing by doing

    preg_replace('/&#10/','',$string);
    

    You can also run an array through preg_replace like

    $string = $myFile;
    $search = array('/&#10/','/sec/');
    $replace = array('','<alt>');
    preg_replace($search,$replace,$string);
    

    This may also be relevant to you.