lib_text.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. function bbDecode($string) {
  3. $find = array(
  4. "'\[b\](.*?)\[/b\]'is",
  5. "'\[u\](.*?)\[/u\]'is",
  6. "'\[i\](.*?)\[/i\]'is",
  7. "'\[s\](.*?)\[/s\]'is",
  8. "'\[code\](.*?)\[/code\]'is",
  9. "'\[quote\](.*?)\[/quote\]'is",
  10. "'\[quote=(.*?)\](.*?)\[/quote\]'is",
  11. "'\[span=(.*?)\](.*?)\[/span\]'i",
  12. "'\[div=(.*?)\](.*?)\[/div\]'is",
  13. "'\[h\](.*?)\[/h\]'i",
  14. "'\[url\](.*?)\[/url\]'i",
  15. "'\[url=(.*?)\](.*?)\[/url\]'i",
  16. "'\[video\](.*?)\[/video\]'i",
  17. "'\[video width=(.*?) height=(.*?)\](.*?)\[/video\]'i",
  18. "'\[img\](.*?)\[/img\]'i",
  19. "'\[img title=(.*?) rel=(.*?)\](.*?)\[/img\]'i",
  20. "'\[img title=(.*?)\](.*?)\[/img\]'i",
  21. );
  22. $replace = array(
  23. "<strong>\\1</strong>",
  24. "<u>\\1</u>",
  25. "<i>\\1</i>",
  26. "<del>\\1</del>",
  27. "<pre>\\1</pre>",
  28. "<q>\\1</q>",
  29. "<q><span class=\"cite\">\\1 a écrit</span><br />\\2</q>",
  30. "<span class=\"\\1\">\\2</span>",
  31. "<div class=\"\\1\">\\2</div>",
  32. "<b>\\1</b><br />",
  33. "<a href=\"\\1\">\\1</a>",
  34. "<a href=\"\\1\">\\2</a>",
  35. "<object width=\"480\" height=\"387\" class=\"center\"><param name=\"movie\" value=\"\\1\"></param><embed src=\"\\1\" type=\"application/x-shockwave-flash\" width=\"480\" height=\"387\"></embed></object>",
  36. "<object width=\"\\1\" height=\"\\2\" class=\"center\"><param name=\"movie\" value=\"\\3\"></param><embed src=\"\\3\" type=\"application/x-shockwave-flash\" width=\"\\1\" height=\"\\2\"></embed></object>",
  37. "<a href=\"\\1\" rel=\"prettyPhoto\"><img src=\"\\1\" alt=\"\" /></a>",
  38. "<img class=\"illustration\" src=\"\\3\" alt=\"\\1\" />",
  39. "<img src=\"\\2\" alt=\"\\1\" />",
  40. );
  41. $string = makeLinks(preg_replace ($find, $replace, $string));
  42. $string = nl2brPlus ($string);
  43. return $string;
  44. }
  45. // do nl2br except when in a <pre> tag
  46. function nl2brPlus($string) {
  47. $string = str_replace("\n", "<br />", $string);
  48. if(preg_match_all('/\<pre\>(.*?)\<\/pre\>/', $string, $match)){
  49. foreach($match as $a){
  50. foreach($a as $b){
  51. $string = str_replace('<pre>'.$b.'</pre>', "<pre>".str_replace("<br />", "", $b)."</pre>", $string);
  52. }
  53. }
  54. }
  55. return $string;
  56. }
  57. # Transform URL and e-mails into links
  58. function makeLinks($string) {
  59. $string = preg_replace_callback('/\s(http|https|ftp):(\/\/){0,1}([^\"\s]*)/i','splitUri',$string);
  60. return $string;
  61. }
  62. # Split links, require for makeLinks
  63. function splitUri($matches) {
  64. $uri = $matches[1].':'.$matches[2].$matches[3];
  65. $t = parse_url($uri);
  66. $link = $matches[3];
  67. if (!empty($t['scheme'])) {
  68. return ' <a href="'.$uri.'">'.$link.'</a>';
  69. } else {
  70. return $uri;
  71. }
  72. }
  73. // parse la description pour ajouter les liens sur les tags
  74. function parse_tags ($desc) {
  75. $desc_parse = preg_replace ('/#([\w\dÀÇÈÉÊËÎÏÔÙÚÛÜàáâçèéêëîïóùúûü]+)/i', '<a class="linktag" href="?addtag=\\1">\\1</a>', $desc);
  76. return $desc_parse;
  77. }