WYSIWYG text to Flash Supported


/ Published in: PHP
Save to your folder(s)

This class is used to convert any WYSIWYG text to flash friendly text.

Hope you people would like that.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3.  
  4. // Usage:
  5. // $flashString = $objFlahTags->makeFlashFriendlyStr($str);
  6.  
  7. class flashTags {
  8.  
  9. function rgb2hex($r, $g=-1, $b=-1) {
  10. if (is_array($r) && sizeof($r) == 3)
  11. list($r, $g, $b) = $r;
  12.  
  13. $r = intval($r);
  14. $g = intval($g);
  15. $b = intval($b);
  16.  
  17. $r = dechex($r < 0 ? 0 : ($r > 255 ? 255 : $r));
  18. $g = dechex($g < 0 ? 0 : ($g > 255 ? 255 : $g));
  19. $b = dechex($b < 0 ? 0 : ($b > 255 ? 255 : $b));
  20.  
  21. $color = (strlen($r) < 2 ? '0' : '') . $r;
  22. $color .= ( strlen($g) < 2 ? '0' : '') . $g;
  23. $color .= ( strlen($b) < 2 ? '0' : '') . $b;
  24. return '#' . $color;
  25. }
  26.  
  27. function getAttribute($attrib, $tag) {
  28. //get attribute from html tag
  29. $re = '/' . $attrib . '=["\']?([^"\' ]*)["\' ]/is';
  30.  
  31. preg_match($re, $tag, $match);
  32.  
  33. if ($match)
  34. $return = $match[1];
  35. else
  36. $return = false;
  37.  
  38. return $return;
  39. }
  40.  
  41. function getReplacesArray($match0, $match1) {
  42. $replaceFrom = $match0;
  43.  
  44. preg_match_all('/<span[^>]*>/i', $match0, $span_full, PREG_SET_ORDER);
  45.  
  46. $span_elem = $span_full[0][0];
  47.  
  48. if (!preg_match_all('/"[^>]*"/i', $span_elem, $span_attrs, PREG_SET_ORDER)) {
  49. return array($match0, $match1);
  50. }
  51.  
  52. $span_attrs = str_replace(array('"', ' '), '', $span_attrs[0][0]);
  53.  
  54. $span_attrs_parts = explode(';', $span_attrs);
  55.  
  56. $len = count($span_attrs_parts);
  57.  
  58. $startTags = array();
  59.  
  60. $endTags = array();
  61.  
  62. for ($j = 0; $j < $len; $j++) {
  63. $single_attr = $span_attrs_parts[$j];
  64.  
  65. if (!empty($single_attr)) {
  66. if (strtolower($single_attr) == 'font-weight:bold') {
  67. $startTags[] = '<b>';
  68. $endTags[] = '</b>';
  69. } else if (strtolower($single_attr) == 'font-style:italic') {
  70. $startTags[] = '<i>';
  71. $endTags[] = '</i>';
  72. } else if (strtolower($single_attr) == 'text-decoration:underline') {
  73. $startTags[] = '<u>';
  74. $endTags[] = '</u>';
  75. } else if (stristr($single_attr, 'color:')) {
  76. $colorHex = str_ireplace('color:', '', $single_attr);
  77.  
  78. if (stristr($colorHex, 'rgb(')) {
  79. $rgbVal = str_ireplace(array('rgb(', ')'), '', $colorHex);
  80.  
  81. $rgbParts = explode(',', $rgbVal);
  82.  
  83. $partR = $rgbParts[0];
  84.  
  85. $partG = isset($rgbParts[1]) ? $rgbParts[1] : -1;
  86.  
  87. $partB = isset($rgbParts[2]) ? $rgbParts[2] : -1;
  88.  
  89. $colorHex = $this->rgb2hex($partR, $partG, $partB);
  90. }
  91.  
  92. $startTags[] = "<font color='$colorHex'>";
  93. $endTags[] = "</font>";
  94. }
  95. }
  96. }
  97.  
  98. krsort($endTags);
  99.  
  100. $startTags = implode('', $startTags);
  101.  
  102. $endTags = implode('', $endTags);
  103.  
  104. $replaceTo = $startTags . $match1 . $endTags;
  105.  
  106. return array($replaceFrom, $replaceTo);
  107. }
  108.  
  109. function getArrReplacesArray($matches) {
  110. $totalMatches = count($matches);
  111.  
  112. $arrReplaceFrom = array();
  113.  
  114. $arrReplaceTo = array();
  115.  
  116. if ($totalMatches > 0) {
  117. for ($i = 0; $i < $totalMatches; $i++) {
  118. $match0 = $matches[$i][0];
  119.  
  120. $match1 = $matches[$i][1];
  121.  
  122. if (stristr($match1, '<span')) {
  123. preg_match_all('|<span[^>]+>(.*)<span|U', $match0, $matches2, PREG_SET_ORDER);
  124.  
  125. $newStr = str_replace($matches2[0][0], '<span', $match0);
  126.  
  127. preg_match_all('|<span[^>]+>(.*)</span>|U', $newStr, $matches3, PREG_SET_ORDER);
  128.  
  129. list($replaceFrom, $replaceTo) = $this->getReplacesArray($matches3[0][0], $matches3[0][1]);
  130. }
  131. else
  132. list($replaceFrom, $replaceTo) = $this->getReplacesArray($match0, $match1);
  133.  
  134. $arrReplaceFrom[] = $replaceFrom;
  135.  
  136. $arrReplaceTo[] = $replaceTo;
  137. }
  138. }
  139.  
  140. return array($arrReplaceFrom, $arrReplaceTo);
  141. }
  142.  
  143. function replaceAllAnchorStyles($str) {
  144. preg_match_all('|<a[^>]+>(.*)</a>|U', $str, $matches, PREG_SET_ORDER);
  145.  
  146. $totalMatches = count($matches);
  147.  
  148. $arrReplaceFrom = array();
  149.  
  150. $arrReplaceTo = array();
  151.  
  152. if ($totalMatches) {
  153. for ($i = 0; $i < $totalMatches; $i++) {
  154. $match0 = $matches[$i][0];
  155.  
  156. preg_match_all('|style="(.*)"|U', $match0, $matches1, PREG_SET_ORDER);
  157.  
  158. $href = $this->getAttribute('href', $match0);
  159.  
  160. $target = $this->getAttribute('target', $match0);
  161.  
  162. $title = $this->getAttribute('title', $match0);
  163.  
  164. if (count($matches1)) {
  165. $match1 = $matches1[0][1];
  166.  
  167. $match1 = trim($match1);
  168.  
  169. $match1 = str_replace(' ', '', $match1);
  170.  
  171. $match1 = strtolower($match1);
  172.  
  173. if ($match1) {
  174. $attrs_parts = explode(';', $match1);
  175.  
  176. $len = count($attrs_parts);
  177.  
  178. $startTag = "<u><a";
  179.  
  180. if ($href)
  181. $startTag .= " href='$href'";
  182.  
  183. if ($target)
  184. $startTag .= " target='$target'";
  185.  
  186. if ($title)
  187. $startTag .= " title='$title'";
  188.  
  189. $startTag .= ">";
  190.  
  191. $startTags = array($startTag);
  192.  
  193. $endTags = array("</a></u>");
  194.  
  195. for ($j = 0; $j < $len; $j++) {
  196. $single_attr = $attrs_parts[$j];
  197.  
  198. if (!empty($single_attr)) {
  199. if (strtolower($single_attr) == 'font-weight:bold') {
  200. $startTags[] = '<b>';
  201. $endTags[] = '</b>';
  202. } else if (strtolower($single_attr) == 'font-style:italic') {
  203. $startTags[] = '<i>';
  204. $endTags[] = '</i>';
  205. } else if (strtolower($single_attr) == 'text-decoration:underline') {
  206. $startTags[] = '<u>';
  207. $endTags[] = '</u>';
  208. } else if (stristr($single_attr, 'color:')) {
  209. $colorHex = str_ireplace('color:', '', $single_attr);
  210.  
  211. if (stristr($colorHex, 'rgb(')) {
  212. $rgbVal = str_ireplace(array('rgb(', ')'), '', $colorHex);
  213.  
  214. $rgbParts = explode(',', $rgbVal);
  215.  
  216. $partR = $rgbParts[0];
  217.  
  218. $partG = isset($rgbParts[1]) ? $rgbParts[1] : -1;
  219.  
  220. $partB = isset($rgbParts[2]) ? $rgbParts[2] : -1;
  221.  
  222. $colorHex = $this->rgb2hex($partR, $partG, $partB);
  223. }
  224.  
  225. $startTags[] = "<font color='$colorHex'>";
  226. $endTags[] = "</font>";
  227. }
  228. }
  229. }
  230.  
  231. krsort($endTags);
  232.  
  233. $startTags = implode('', $startTags);
  234.  
  235. $endTags = implode('', $endTags);
  236.  
  237. $replaceTo = $startTags . $matches[$i][1] . $endTags;
  238.  
  239. $arrReplaceFrom[] = $match0;
  240.  
  241. $arrReplaceTo[] = $replaceTo;
  242. }
  243. }
  244. }
  245. }
  246.  
  247. $str = str_ireplace($arrReplaceFrom, $arrReplaceTo, $str);
  248.  
  249. return $str;
  250. }
  251.  
  252. function makeFlashFriendlyStr($str) {
  253.  
  254. $str = str_ireplace('<span>', '<span >', $str);
  255. $dummyBrText = '~d~u~m~m~y~b~r~';
  256.  
  257. $str_wo_br = preg_replace("/<br[^>]*>/is", $dummyBrText, $str);
  258.  
  259. $str = $str_wo_br;
  260.  
  261. do {
  262. preg_match_all('|<span[^>]+>(.*)</span>|U', $str, $matches, PREG_SET_ORDER);
  263.  
  264. list($arrReplaceFrom, $arrReplaceTo) = $this->getArrReplacesArray($matches);
  265.  
  266. $str = str_replace($arrReplaceFrom, $arrReplaceTo, $str);
  267. } while ($matches);
  268.  
  269. $str = $this->replaceAllAnchorStyles($str);
  270.  
  271. $str = str_replace(array($dummyBrText, '"'), array(PHP_EOL, '"'), $str);
  272.  
  273.  
  274. $str = "<TEXTFORMAT LEADING='0'><font face='Verdana' size='13'>$str</font></TEXTFORMAT>";
  275.  
  276. return $str;
  277. }
  278.  
  279. }
  280.  
  281. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.