Newscoop\Entity\Comment::formatMessage PHP Метод

formatMessage() защищенный Метод

Method used to format message from comments
protected formatMessage ( string $str ) : string
$str string
Результат string
    protected function formatMessage($str)
    {
        $parts = explode('<', $str);
        // if no < was found then return the original string
        if (count($parts) === 1) {
            return $str;
        }
        /** @type array vector where the tag list are keeped */
        $tag = array();
        $attrib = array();
        $contentAfter = array(0 => $parts[0]);
        for ($i = 1, $counti = count($parts); $i < $counti; $i++) {
            $tagAndContent = explode('>', $parts[$i], 2);
            $tagAndAttrib = explode(' ', $tagAndContent[0], 2);
            if (isset($tagAndAttrib[1])) {
                /**
                 * @todo make a better attributes filter regex
                 *      this is breaking on not quotes define attibutes
                 *      ex: like checked=true for good parsing should be checked="true" or checked='true'
                 */
                preg_match_all("#([^=]+)=\\s*(['\"])?([^\\2]*)\\2#iU", $tagAndAttrib[1], $rez);
                if (isset($rez[1])) {
                    for ($k = 0, $countk = count($rez[1]); $k < $countk; $k++) {
                        $attrib[$i][] = array(strtolower(trim($rez[1][$k])), $rez[3][$k]);
                    }
                }
            }
            $tag[$i] = $tagAndAttrib[0];
            $contentAfter[$i] = isset($tagAndContent[1]) ? $tagAndContent[1] : '';
        }
        $closed = $tag;
        $return = '';
        $allowedNameTags = array_keys($this->allowedTags);
        for ($i = 0, $counti = count($contentAfter); $i < $counti; $i++) {
            $isClosed = isset($tag[$i]) ? substr($tag[$i], 0, 1) == '/' : false;
            if (isset($tag[$i])) {
                $tagName = $tag[$i];
                if (substr($tagName, -1, 1) == '/') {
                    $tagName = substr($tagName, 0, -1);
                }
            }
            if (isset($tag[$i]) && in_array($tagName, $allowedNameTags)) {
                unset($closed[$i]);
                $good = array_search('/' . $tag[$i], $closed, true);
                if ($good) {
                    unset($closed[$good]);
                    $composeTag = '<' . $tag[$i] . ' ';
                    if (isset($attrib[$i])) {
                        for ($j = 0, $countj = count($attrib[$i]); $j < $countj; $j++) {
                            if ($attrib[$i][$j][0] == 'href') {
                            }
                            $attrib[$i][$j][1] = preg_replace('/(javascript[:]?)/i', '', $attrib[$i][$j][1]);
                            if (in_array($attrib[$i][$j][0], $this->allowedTags[$tag[$i]])) {
                                $composeTag .= $attrib[$i][$j][0] . '="' . $attrib[$i][$j][1] . '" ';
                            }
                        }
                    }
                    $return .= substr($composeTag, 0, -1) . '>' . $contentAfter[$i];
                } else {
                    $composeTag = '<' . $tag[$i] . ' ';
                    $title = false;
                    $cite = false;
                    if (isset($attrib[$i])) {
                        for ($j = 0, $countj = count($attrib[$i]); $j < $countj; $j++) {
                            if (in_array($attrib[$i][$j][0], $this->allowedTags[$tag[$i]])) {
                                if ($attrib[$i][$j][0] == 'href') {
                                    $attrib[$i][$j][1] = preg_replace('/(javascript[:]?)/i', '', $attrib[$i][$j][1]);
                                }
                                if ($attrib[$i][$j][0] == 'title') {
                                    $title = $attrib[$i][$j][1];
                                } elseif ($attrib[$i][$j][0] == 'cite') {
                                    $cite = $attrib[$i][$j][1];
                                } else {
                                    $composeTag .= $attrib[$i][$j][0] . '="' . $attrib[$i][$j][1] . '" ';
                                }
                            }
                        }
                    }
                    // if title is set and is a broken tag use the title like inline text
                    if (in_array($tagName, $this->allowedEmpty)) {
                        $return .= substr($composeTag, 0, -1) . '>' . $contentAfter[$i];
                    } elseif ($title !== false) {
                        $return .= substr($composeTag, 0, -1) . '>' . $title . '</' . $tag[$i] . '>' . $contentAfter[$i];
                    } elseif ($cite !== false) {
                        $return .= substr($composeTag, 0, -1) . '>' . $cite . '</' . $tag[$i] . '>' . $contentAfter[$i];
                    } else {
                        $return .= substr($composeTag, 0, -1) . '>' . $contentAfter[$i] . '</' . $tag[$i] . '>';
                    }
                }
            } elseif (isset($tag[$i]) && $isClosed && in_array(substr($tag[$i], 1), $allowedNameTags)) {
                unset($closed[$i]);
                $return .= '<' . $tag[$i] . '>' . $contentAfter[$i];
            } else {
                $return .= $contentAfter[$i];
            }
        }
        return $return;
    }