Return to Snippet

Revision: 32671
at October 1, 2010 17:25 by aamirrajpoot


Initial Code
<?php


// Usage:
// $flashString = $objFlahTags->makeFlashFriendlyStr($str);

class flashTags {

    function rgb2hex($r, $g=-1, $b=-1) {
        if (is_array($r) && sizeof($r) == 3)
            list($r, $g, $b) = $r;

        $r = intval($r);
        $g = intval($g);
        $b = intval($b);

        $r = dechex($r < 0 ? 0 : ($r > 255 ? 255 : $r));
        $g = dechex($g < 0 ? 0 : ($g > 255 ? 255 : $g));
        $b = dechex($b < 0 ? 0 : ($b > 255 ? 255 : $b));

        $color = (strlen($r) < 2 ? '0' : '') . $r;
        $color .= ( strlen($g) < 2 ? '0' : '') . $g;
        $color .= ( strlen($b) < 2 ? '0' : '') . $b;
        return '#' . $color;
    }

    function getAttribute($attrib, $tag) {
        //get attribute from html tag
        $re = '/' . $attrib . '=["\']?([^"\' ]*)["\' ]/is';

        preg_match($re, $tag, $match);

        if ($match)
            $return = $match[1];
        else
            $return = false;

        return $return;
    }

    function getReplacesArray($match0, $match1) {
        $replaceFrom = $match0;

        preg_match_all('/<span[^>]*>/i', $match0, $span_full, PREG_SET_ORDER);

        $span_elem = $span_full[0][0];

        if (!preg_match_all('/"[^>]*"/i', $span_elem, $span_attrs, PREG_SET_ORDER)) {
            return array($match0, $match1);
        }

        $span_attrs = str_replace(array('"', ' '), '', $span_attrs[0][0]);

        $span_attrs_parts = explode(';', $span_attrs);

        $len = count($span_attrs_parts);

        $startTags = array();

        $endTags = array();

        for ($j = 0; $j < $len; $j++) {
            $single_attr = $span_attrs_parts[$j];

            if (!empty($single_attr)) {
                if (strtolower($single_attr) == 'font-weight:bold') {
                    $startTags[] = '<b>';
                    $endTags[] = '</b>';
                } else if (strtolower($single_attr) == 'font-style:italic') {
                    $startTags[] = '<i>';
                    $endTags[] = '</i>';
                } else if (strtolower($single_attr) == 'text-decoration:underline') {
                    $startTags[] = '<u>';
                    $endTags[] = '</u>';
                } else if (stristr($single_attr, 'color:')) {
                    $colorHex = str_ireplace('color:', '', $single_attr);

                    if (stristr($colorHex, 'rgb(')) {
                        $rgbVal = str_ireplace(array('rgb(', ')'), '', $colorHex);

                        $rgbParts = explode(',', $rgbVal);

                        $partR = $rgbParts[0];

                        $partG = isset($rgbParts[1]) ? $rgbParts[1] : -1;

                        $partB = isset($rgbParts[2]) ? $rgbParts[2] : -1;

                        $colorHex = $this->rgb2hex($partR, $partG, $partB);
                    }

                    $startTags[] = "<font color='$colorHex'>";
                    $endTags[] = "</font>";
                }
            }
        }

        krsort($endTags);

        $startTags = implode('', $startTags);

        $endTags = implode('', $endTags);

        $replaceTo = $startTags . $match1 . $endTags;

        return array($replaceFrom, $replaceTo);
    }

    function getArrReplacesArray($matches) {
        $totalMatches = count($matches);

        $arrReplaceFrom = array();

        $arrReplaceTo = array();

        if ($totalMatches > 0) {
            for ($i = 0; $i < $totalMatches; $i++) {
                $match0 = $matches[$i][0];

                $match1 = $matches[$i][1];

                if (stristr($match1, '<span')) {
                    preg_match_all('|<span[^>]+>(.*)<span|U', $match0, $matches2, PREG_SET_ORDER);

                    $newStr = str_replace($matches2[0][0], '<span', $match0);

                    preg_match_all('|<span[^>]+>(.*)</span>|U', $newStr, $matches3, PREG_SET_ORDER);

                    list($replaceFrom, $replaceTo) = $this->getReplacesArray($matches3[0][0], $matches3[0][1]);
                }
                else
                    list($replaceFrom, $replaceTo) = $this->getReplacesArray($match0, $match1);

                $arrReplaceFrom[] = $replaceFrom;

                $arrReplaceTo[] = $replaceTo;
            }
        }

        return array($arrReplaceFrom, $arrReplaceTo);
    }

    function replaceAllAnchorStyles($str) {
        preg_match_all('|<a[^>]+>(.*)</a>|U', $str, $matches, PREG_SET_ORDER);

        $totalMatches = count($matches);

        $arrReplaceFrom = array();

        $arrReplaceTo = array();

        if ($totalMatches) {
            for ($i = 0; $i < $totalMatches; $i++) {
                $match0 = $matches[$i][0];

                preg_match_all('|style="(.*)"|U', $match0, $matches1, PREG_SET_ORDER);

                $href = $this->getAttribute('href', $match0);

                $target = $this->getAttribute('target', $match0);

                $title = $this->getAttribute('title', $match0);

                if (count($matches1)) {
                    $match1 = $matches1[0][1];

                    $match1 = trim($match1);

                    $match1 = str_replace(' ', '', $match1);

                    $match1 = strtolower($match1);

                    if ($match1) {
                        $attrs_parts = explode(';', $match1);

                        $len = count($attrs_parts);

                        $startTag = "<u><a";

                        if ($href)
                            $startTag .= " href='$href'";

                        if ($target)
                            $startTag .= " target='$target'";

                        if ($title)
                            $startTag .= " title='$title'";

                        $startTag .= ">";

                        $startTags = array($startTag);

                        $endTags = array("</a></u>");

                        for ($j = 0; $j < $len; $j++) {
                            $single_attr = $attrs_parts[$j];

                            if (!empty($single_attr)) {
                                if (strtolower($single_attr) == 'font-weight:bold') {
                                    $startTags[] = '<b>';
                                    $endTags[] = '</b>';
                                } else if (strtolower($single_attr) == 'font-style:italic') {
                                    $startTags[] = '<i>';
                                    $endTags[] = '</i>';
                                } else if (strtolower($single_attr) == 'text-decoration:underline') {
                                    $startTags[] = '<u>';
                                    $endTags[] = '</u>';
                                } else if (stristr($single_attr, 'color:')) {
                                    $colorHex = str_ireplace('color:', '', $single_attr);

                                    if (stristr($colorHex, 'rgb(')) {
                                        $rgbVal = str_ireplace(array('rgb(', ')'), '', $colorHex);

                                        $rgbParts = explode(',', $rgbVal);

                                        $partR = $rgbParts[0];

                                        $partG = isset($rgbParts[1]) ? $rgbParts[1] : -1;

                                        $partB = isset($rgbParts[2]) ? $rgbParts[2] : -1;

                                        $colorHex = $this->rgb2hex($partR, $partG, $partB);
                                    }

                                    $startTags[] = "<font color='$colorHex'>";
                                    $endTags[] = "</font>";
                                }
                            }
                        }

                        krsort($endTags);

                        $startTags = implode('', $startTags);

                        $endTags = implode('', $endTags);

                        $replaceTo = $startTags . $matches[$i][1] . $endTags;

                        $arrReplaceFrom[] = $match0;

                        $arrReplaceTo[] = $replaceTo;
                    }
                }
            }
        }

        $str = str_ireplace($arrReplaceFrom, $arrReplaceTo, $str);

        return $str;
    }

    function makeFlashFriendlyStr($str) {
        
        $str = str_ireplace('<span>', '<span >', $str);
        $dummyBrText = '~d~u~m~m~y~b~r~';

        $str_wo_br = preg_replace("/<br[^>]*>/is", $dummyBrText, $str);

        $str = $str_wo_br;

        do {
            preg_match_all('|<span[^>]+>(.*)</span>|U', $str, $matches, PREG_SET_ORDER);

            list($arrReplaceFrom, $arrReplaceTo) = $this->getArrReplacesArray($matches);

            $str = str_replace($arrReplaceFrom, $arrReplaceTo, $str);
        } while ($matches);

        $str = $this->replaceAllAnchorStyles($str);

        $str = str_replace(array($dummyBrText, '"'), array(PHP_EOL, '"'), $str);

        
        $str = "<TEXTFORMAT LEADING='0'><font face='Verdana' size='13'>$str</font></TEXTFORMAT>";

        return $str;
    }

}

?>

Initial URL


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

Hope you people would like that.

Initial Title
WYSIWYG text to Flash Supported

Initial Tags
flash

Initial Language
PHP