[resolu] Nettoyer du code html pour enlever certaines balise inutiles

Nettoyer du code html pour enlever certaines balise inutiles [resolu] - PHP - Programmation

Marsh Posté le 10-05-2012 à 18:49:42    

Je souhaite nettoyer le code html entré par des utilisateurs via un éditeur Javascript (TinyMCe pour ne pas le citer ^^).
Quand ils font des copier / coller depuis un outil texte, j'ai du formatage indésirable (notamment changement de taille et typo).
J'arrive à remplacer les styles indésirable par des balises vides, par contre je n'arrive pas à me débarrasser des balises vides inutiles.
 
J'ai fait une page de démo que vous pouvez consulter pour le voir le fonctionnement déconnant :
http://mbillonlanfrey.free.fr/preg_replace_callback/
 
Et voici le code de ma page (comme vous pouvez le voir dans le code vous pouvez fournir une autre regex en paramètre $_GET) :

Code :
  1. <?
  2. $debug = '';
  3. $i = 0;
  4. $regex = '%<span>((?:[^<]|<(?!/?span> )|(?R))+)</span>%';
  5. if (array_key_exists('regex', $_GET)) {
  6. $regex = $_GET['regex'];
  7. }
  8. function CleanSpanRecursive($content) {
  9. global $debug, $i, $regex;
  10. $debug .= 'CleanSpanRecursive('.$i.') => ';
  11. $i++;
  12. if (is_array($content)) {
  13.  $content = $content[1];
  14.  $debug .= 'is_array = 1 ';
  15. } else $debug .= 'is_array = 0 ';
  16. $debug .= htmlentities($content)."\n\n";
  17. return preg_replace_callback($regex, 'CleanSpanRecursive', $content);
  18. }
  19. function CleanHtml($content, $keep_color = true) {
  20. global $debug;
  21. //nettoyage contenu
  22. if ($keep_color) {
  23.  $content = preg_replace('/<span style="(font-(family|size|weight)):([a-z0-9-,\s]*);">/isU', '<span>', $content);
  24. } else {
  25.  $content = preg_replace('/<span style="(color|font-(family|size|weight)):([a-z0-9-,\s#]*);">/isU', '<span>', $content);
  26. }
  27. $debug .= 'CleanHtml => '.htmlentities($content)."\n\n";
  28. //nettoyage span inutile
  29. return CleanSpanRecursive($content);
  30. }
  31. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  32. <html xmlns="http://www.w3.org/1999/xhtml">
  33. <head>
  34. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  35. <title>Test preg_replace_callback()</title>
  36. </head>
  37. <body>
  38. <a href="http://fr.php.net/manual/fr/function.preg-replace-callback.php">preg_replace_callback('<?=htmlentities($regex)?>', 'CleanSpanRecursive', $content)</a>
  39. <pre>
  40. <?
  41. $content = '
  42. <p><span style="font-family: Trebuchet MS,sans-serif;">
  43. <span style="font-size: x-small;">
  44.  <span style="color: #000000;">
  45.   <span style="font-family: Times New Roman,serif;">
  46.    <span style="font-size: small;">
  47.     <span style="font-weight: normal;">
  48.      Blah
  49.      <span style="text-decoration: underline;">
  50.       blah
  51.      </span>
  52.      blah
  53.     </span>
  54.    </span>
  55.   </span>
  56.  </span>
  57. </span>
  58. </span></p>
  59. <p><span style="font-weight: normal;">
  60. Blah
  61. <span style="text-decoration: underline;">
  62.  blah
  63. </span>
  64. blah
  65. </span>
  66. <span style="font-weight: x-small;">
  67. Blah
  68. <span style="color: #000000;">
  69.  blah
  70. </span>
  71. blah
  72. </span></p>';
  73. $debug .= "\n\nDEBUT : ".htmlentities($content)."\n\n";
  74. $content = CleanHtml($content);
  75. $debug .= "\n\nFIN : ".htmlentities($content)."\n\n";
  76. echo $debug;
  77. ?>
  78. </pre>
  79. </body>
  80. </html>


 
Un grand merci à celui qui me sortira de l'ornière...


Message édité par mechkurt le 12-05-2012 à 18:52:24
Reply

Marsh Posté le 10-05-2012 à 18:49:42   

Reply

Marsh Posté le 12-05-2012 à 18:51:01    

Bon ben impossible de trouver une solution en regex pur, je suis passé par php DOM...

Code :
  1. function CleanHtml($content, $remove_color = false, $clean_span = false) {
  2. $content = trim($content);
  3. if (empty($content)) {
  4.  return '';
  5. }
  6. //nettoyage contenu
  7. if ($remove_color) {
  8.  $content = preg_replace('/<span style="(color|font-(family|size|weight)):([a-z0-9-,\s#]*);">/isU', '<span class="delete_me">', $content, -1, $count);
  9. } else {
  10.  $content = preg_replace('/<span style="(font-(family|size|weight)):([a-z0-9-,\s]*);">/isU', '<span class="delete_me">', $content, -1, $count);
  11. }
  12. if ($clean_span && !empty($count)) {
  13.  //si on nettoie les span inutiles
  14.  $xml = new DOMDocument();
  15.  $xml->loadXML('<?xml version="1.0" encoding="utf-8"?><root>'."\n".$content."\n".'</root>');
  16.  $xpath = new DOMXpath($xml);
  17.  $spans = $xpath->query('//span[@class="delete_me"]');
  18.  if (!is_null($spans)) {
  19.   foreach($spans as $from) {
  20.    $sibling = $from->firstChild;
  21.    do {
  22.     $next = $sibling->nextSibling;
  23.     $from->parentNode->insertBefore($sibling, $from);
  24.    } while ($sibling = $next);
  25.    $from->parentNode->removeChild($from);
  26.   }
  27.  }
  28.  $content = substr($xml->saveXML(),46,-9);
  29. }
  30. //nettoyage escpaces vides multiples
  31. return preg_replace('/\s\s+/', ' ', $content);
  32. }
  33. $content = '
  34. <p><span style="font-family: Trebuchet MS,sans-serif;">
  35. <span style="font-size: x-small;">
  36.  <span style="color: #FF0000;">
  37.   <span style="font-family: Times New Roman,serif;">
  38.    <span style="font-size: small;">
  39.     <span style="font-weight: normal;">
  40.      ABlah1
  41.      <span style="text-decoration: underline;">
  42.       blah2
  43.      </span>
  44.      blah3
  45.     </span>
  46.    </span>
  47.   </span>
  48.  </span>
  49. </span>
  50. </span></p>
  51. <p><span style="font-weight: normal;">
  52. BBlah1
  53. <span style="text-decoration: underline;">
  54.  blah2
  55. </span>
  56. blah3
  57. </span>
  58. <br/>
  59. <span style="color: #FF0000;">
  60. CBlah1
  61. <span style="font-size: x-large;">
  62.  blah2
  63. </span>
  64. blah3
  65. </span></p>';
  66. echo CleanHtml($content);

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed