parsingHTML.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. /**
  3. * Logiciel : HTML2PDF - classe ParsingHTML
  4. *
  5. * Convertisseur HTML => PDF, utilise fpdf de Olivier PLATHEY
  6. * Distribu� sous la licence LGPL.
  7. *
  8. * @author Laurent MINGUET <webmaster@spipu.net>
  9. * @version 3.24 - 05/08/2009
  10. */
  11. if (!defined('__CLASS_PARSINGHTML__'))
  12. {
  13. define('__CLASS_PARSINGHTML__', true);
  14. class parsingHTML
  15. {
  16. var $html = ''; // code HTML � parser
  17. var $code = array(); // code HTML pars�
  18. var $num = 0; // num�ro de table
  19. var $level = 0; // niveaux de table
  20. /**
  21. * Constructeur
  22. *
  23. * @return null
  24. */
  25. function parsingHTML()
  26. {
  27. $this->num = 0;
  28. $this->level = array($this->num);
  29. $this->html = '';
  30. $this->code = array();
  31. }
  32. /**
  33. * D�finir le code HTML � parser
  34. *
  35. * @param string code html
  36. * @return null
  37. */
  38. function setHTML($html)
  39. {
  40. $html = preg_replace('/<!--(.*)-->/isU', '', $html);
  41. $this->html = $html;
  42. }
  43. /**
  44. * parser le code HTML
  45. *
  46. * @return null
  47. */
  48. function parse()
  49. {
  50. $parents = array();
  51. // r�cup�rer le code � parser
  52. $content = $this->html;
  53. // chercher les balises HTML du code
  54. $tmp = array();
  55. $this->searchCode($content, $tmp);
  56. // identifier les balises une � une
  57. $pre_in = false;
  58. $pre_br = array(
  59. 'name' => 'br',
  60. 'close' => false,
  61. 'param' => array(
  62. 'style' => array(),
  63. 'num' => 0
  64. )
  65. );
  66. $todos = array();
  67. foreach($tmp as $part)
  68. {
  69. // si c'est un texte
  70. if ($part[0]=='txt')
  71. {
  72. // enregistrer l'action correspondante
  73. if (!$pre_in)
  74. {
  75. if (trim($part[1])!=='')
  76. {
  77. // remplacer tous les espaces, tabulations, saufs de ligne multiples par de simples espaces
  78. $part[1] = preg_replace('/([\s]+)/is', ' ', $part[1]);
  79. $todos[] = array(
  80. 'name' => 'write',
  81. 'close' => false,
  82. 'param' => array('txt' => $part[1]),
  83. );
  84. }
  85. }
  86. else
  87. {
  88. $part[1] = str_replace("\r", '', $part[1]);
  89. $part[1] = explode("\n", $part[1]);
  90. foreach($part[1] as $k => $txt)
  91. {
  92. $txt = str_replace("\t", ' ', $txt);
  93. $txt = str_replace(' ', '&nbsp;', $txt);
  94. if ($k>0) $todos[] = $pre_br;
  95. $todos[] = array(
  96. 'name' => 'write',
  97. 'close' => false,
  98. 'param' => array('txt' => $txt),
  99. );
  100. }
  101. }
  102. }
  103. // sinon, analyser le code
  104. else
  105. {
  106. $res = $this->analiseCode($part[1]);
  107. if ($res)
  108. {
  109. if (!in_array($res['name'], array('br', 'hr', 'img', 'input', 'link', 'option')))
  110. {
  111. if ($res['close'])
  112. {
  113. if (count($parents)<1)
  114. @HTML2PDF::makeError(3, __FILE__, __LINE__, $res['name']);
  115. else if ($parents[count($parents)-1]!=$res['name'])
  116. @HTML2PDF::makeError(4, __FILE__, __LINE__, $parents);
  117. else
  118. unset($parents[count($parents)-1]);
  119. }
  120. else
  121. {
  122. $parents[count($parents)] = $res['name'];
  123. }
  124. }
  125. if ($res['name']=='pre' || $res['name']=='code')
  126. {
  127. $pre_in = !$res['close'];
  128. }
  129. $todos[] = $res;
  130. }
  131. }
  132. }
  133. // pour chaque action identifi�e, il faut nettoyer le d�but et la fin des textes
  134. // en fonction des balises qui l'entourent.
  135. $nb = count($todos);
  136. for($k=0; $k<$nb; $k++)
  137. {
  138. //si c'est un texte
  139. if ($todos[$k]['name']=='write')
  140. {
  141. // et qu'une balise sp�cifique le pr�c�de => on nettoye les espaces du d�but du texte
  142. if ($k>0 && in_array($todos[$k-1]['name'], array('table', 'tr', 'td', 'th', 'br', 'div', 'hr', 'p', 'ul', 'ol', 'li')))
  143. $todos[$k]['param']['txt'] = preg_replace('/^([\s]*)([^\s])/isU', '$2', $todos[$k]['param']['txt']);
  144. // et qu'une balise sp�cifique le suit => on nettoye les espaces de la fin du texte
  145. if ($k<count($todos)-1 && in_array($todos[$k+1]['name'], array('table', 'tr', 'td', 'th', 'br', 'div', 'hr', 'p', 'ol', 'ul', 'li')))
  146. $todos[$k]['param']['txt'] = preg_replace('/([^\s])([\s]*)$/isU', '$1', $todos[$k]['param']['txt']);
  147. }
  148. }
  149. if (count($parents)) @HTML2PDF::makeError(5, __FILE__, __LINE__, $parents);
  150. // liste des actions sauv�e
  151. $this->code = $todos;
  152. }
  153. /**
  154. * parser le code HTML
  155. *
  156. * @param string contenu � parser.
  157. * @param &array tableau de retour des donn�es
  158. * @return null
  159. */
  160. function searchCode($content, &$tmp)
  161. {
  162. // s�parer les balises du texte
  163. $tmp = array();
  164. $reg = '/(<[^>]+>)|([^<]+)+/isU';
  165. // pour chaque �l�ment trouv� :
  166. $str = '';
  167. $offset = 0;
  168. while(preg_match($reg, $content, $parse, PREG_OFFSET_CAPTURE, $offset))
  169. {
  170. // si une balise a �t� d�tect�e
  171. if ($parse[1][0])
  172. {
  173. // sauvegarde du texte pr�c�dent si il existe
  174. if ($str!=='') $tmp[] = array('txt',$str);
  175. // sauvegarde de la balise
  176. $tmp[] = array('code',trim($parse[1][0]));
  177. // initialisation du texte suivant
  178. $str = '';
  179. }
  180. else
  181. {
  182. // ajout du texte � la fin de celui qui est d�j� d�tect�
  183. $str.= $parse[2][0];
  184. }
  185. // Update offset to the end of the match
  186. $offset = $parse[0][1] + strlen($parse[0][0]);
  187. unset($parse);
  188. }
  189. // si un texte est pr�sent � la fin, on l'enregistre
  190. if ($str!='') $tmp[] = array('txt',$str);
  191. unset($str);
  192. }
  193. /**
  194. * analyse une balise HTML
  195. *
  196. * @param string code HTML � identifier
  197. * @return array action correspondante
  198. */
  199. function analiseCode($code)
  200. {
  201. // nom de la balise et ouverture ou fermeture
  202. $balise = '<([\/]{0,1})([_a-z0-9]+)([\/>\s]+)';
  203. preg_match('/'.$balise.'/isU', $code, $match);
  204. $close = ($match[1]=='/' ? true : false);
  205. $name = strtolower($match[2]);
  206. // param�tres obligatoires en fonction du nom de la balise
  207. $param = array();
  208. $param['style'] = '';
  209. if ($name=='img') { $param['alt'] = ''; $param['src'] = ''; }
  210. if ($name=='a') { $param['href'] = ''; }
  211. // lecture des param�tres du type nom=valeur
  212. $prop = '([a-zA-Z0-9_]+)=([^"\'\s>]+)';
  213. preg_match_all('/'.$prop.'/is', $code, $match);
  214. for($k=0; $k<count($match[0]); $k++)
  215. $param[trim(strtolower($match[1][$k]))] = trim($match[2][$k]);
  216. // lecture des param�tres du type nom="valeur"
  217. $prop = '([a-zA-Z0-9_]+)=["]([^"]*)["]';
  218. preg_match_all('/'.$prop.'/is', $code, $match);
  219. for($k=0; $k<count($match[0]); $k++)
  220. $param[trim(strtolower($match[1][$k]))] = trim($match[2][$k]);
  221. // lecture des param�tres du type nom='valeur'
  222. $prop = "([a-zA-Z0-9_]+)=[']([^']*)[']";
  223. preg_match_all('/'.$prop.'/is', $code, $match);
  224. for($k=0; $k<count($match[0]); $k++)
  225. $param[trim(strtolower($match[1][$k]))] = trim($match[2][$k]);
  226. // mise en conformit� en style de chaque param�tre
  227. $color = "#000000";
  228. $border = null;
  229. foreach($param as $key => $val)
  230. {
  231. $key = strtolower($key);
  232. switch($key)
  233. {
  234. case 'width':
  235. unset($param[$key]);
  236. $param['style'] = 'width: '.$val.'px; '.$param['style'];
  237. break;
  238. case 'align':
  239. if ($name!=='table')
  240. {
  241. unset($param[$key]);
  242. $param['style'] = 'text-align: '.$val.'; '.$param['style'];
  243. }
  244. break;
  245. case 'valign':
  246. unset($param[$key]);
  247. $param['style'] = 'vertical-align: '.$val.'; '.$param['style'];
  248. break;
  249. case 'height':
  250. unset($param[$key]);
  251. $param['style'] = 'height: '.$val.'px; '.$param['style'];
  252. break;
  253. case 'bgcolor':
  254. unset($param[$key]);
  255. $param['style'] = 'background: '.$val.'; '.$param['style'];
  256. break;
  257. case 'bordercolor':
  258. unset($param[$key]);
  259. $color = $val;
  260. break;
  261. case 'border':
  262. unset($param[$key]);
  263. if (preg_match('/^[0-9]$/isU', $val)) $val = $val.'px';
  264. $border = $val;
  265. break;
  266. case 'cellpadding':
  267. case 'cellspacing':
  268. if (preg_match('/^([0-9]+)$/isU', $val)) $param[$key] = $val.'px';
  269. break;
  270. case 'colspan':
  271. case 'rowspan':
  272. $val = preg_replace('/[^0-9]/isU', '', $val);
  273. if (!$val) $val = 1;
  274. $param[$key] = $val;
  275. break;
  276. }
  277. }
  278. if ($border!==null)
  279. {
  280. if ($border) $param['style'] = 'border: solid '.$border.' '.$color.'; '.$param['style'];
  281. else $param['style'] = 'border: none';
  282. }
  283. // lecture des styles - d�composition
  284. $styles = explode(';', $param['style']);
  285. $param['style'] = array();
  286. foreach($styles as $style)
  287. {
  288. $tmp = explode(':', $style);
  289. if (count($tmp)>1)
  290. {
  291. $cod = $tmp[0]; unset($tmp[0]); $tmp = implode(':', $tmp);
  292. $param['style'][trim(strtolower($cod))] = preg_replace('/[\s]+/isU', ' ', trim($tmp));
  293. }
  294. }
  295. // d�termination du niveau de table pour les ouverture, avec ajout d'un level
  296. if (in_array($name, array('ul', 'ol', 'table')) && !$close)
  297. {
  298. $this->num++;
  299. $this->level[count($this->level)] = $this->num;
  300. }
  301. // attribution du niveau de table o� se trouve l'�l�ment
  302. if (!isset($param['num'])) $param['num'] = $this->level[count($this->level)-1];
  303. // pour les fins de table : suppression d'un level
  304. if (in_array($name, array('ul', 'ol', 'table')) && $close)
  305. {
  306. unset($this->level[count($this->level)-1]);
  307. }
  308. // retour de l'action identifi�e
  309. return array('name' => $name, 'close' => $close ? 1 : 0, 'param' => $param);
  310. }
  311. // r�cup�rer un niveau complet d'HTML entre une ouverture de balise et la fermeture correspondante
  312. function getLevel($k)
  313. {
  314. // si le code n'existe pas : fin
  315. if (!isset($this->code[$k])) return '';
  316. // quelle balise faudra-t-il d�tecter
  317. $detect = $this->code[$k]['name'];
  318. $level = 0; // niveau de profondeur
  319. $end = false; // etat de fin de recherche
  320. $code = ''; // code extrait
  321. // tant que c'est pas fini, on boucle
  322. while (!$end)
  323. {
  324. // action courante
  325. $row = $this->code[$k];
  326. // si write => on ajoute le texte
  327. if ($row['name']=='write')
  328. {
  329. $code.= $row['param']['txt'];
  330. }
  331. // sinon, c'est une balise html
  332. else
  333. {
  334. $not = false; // indicateur de non prise en compte de la balise courante
  335. // si c'est la balise que l'on cherche
  336. if ($row['name']==$detect)
  337. {
  338. if ($level==0) { $not = true; } // si on est � la premiere balise : on l'ignore
  339. $level+= ($row['close'] ? -1 : 1); // modification du niveau en cours en fonction de l'ouvertre / fermeture
  340. if ($level==0) { $not = true; $end = true; } // si on est au niveau 0 : on a fini
  341. }
  342. // si on doit prendre en compte la balise courante
  343. if (!$not)
  344. {
  345. // ecriture du code HTML de la balise
  346. $code.= '<'.($row['close'] ? '/' : '').$row['name'];
  347. foreach($row['param'] as $key => $val)
  348. {
  349. if ($key=='style')
  350. {
  351. $tmp = '';
  352. if (isset($val['text-align'])) unset($val['text-align']);
  353. foreach($val as $ks => $vs) $tmp.= $ks.':'.$vs.'; ';
  354. if (trim($tmp)) $code.= ' '.$key.'="'.$tmp.'"';
  355. }
  356. else
  357. {
  358. $code.= ' '.$key.'="'.$val.'"';
  359. }
  360. }
  361. $code.= '>';
  362. }
  363. }
  364. // on continue tant qu'il y a du code � analyser...
  365. if (isset($this->code[$k+1]))
  366. $k++;
  367. else
  368. $end = true;
  369. }
  370. // retourne la position finale et le code HTML extrait
  371. return array($k, $code);
  372. }
  373. }
  374. }
  375. ?>