Quand on fait un copier/coller d’un article Word dans un éditeur de texte, ça donne quelque chose de ce genre :
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
</xml><![endif]-->
<style>
/* Style Definitions */
table.MsoNormalTable {mso-style-name:"Tableau Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman",serif;}
</style>
<style>
.MsoTruc{font-size:12px;}
.MsoBidule{font-family:Calibri;}
.MsoMachin{microsoft:bof;}
</style>
<p class="MsoNormal" style="margin: 0px; text-align: justify;">Bla, bla, bla...</p>
<p style="mso-fareast-font-family: \'Times New Roman\';">Le texte brut</p>
<p style="plein-de-styles:bien-nazes;"><span><span>Le texte brut</span></span></p>
Ce n’est quand même pas très propre…
Voici donc un petit bout de code pour faire le nettoyage de ces balises et style inutiles :
$texte = 'Votre article Word';
$pattern = array(
'/<!--.*?-->/sim',
'/<style[^>]*>[^<]*?<\/style>/sim',
'/<span[^>]*>/si',
'/<\/span>/si',
'/ style=\'[^\']*\'/si',
'/ style="[^\"]*"/si',
'/ class=\'[^\']*\'/si',
'/ class="[^\"]*"/si',
);
$texte = preg_replace($pattern, '', $texte);
Et voilà le résultat :
<p>Bla, bla, bla...</p> <p>Le texte brut</p> <p>Le texte brut</p>
Aucun commentaire
Soyez le premier à commenter :)