示例#1
0
/**
 * Return a string of HTML, wrapping sections of text in <p>s using newlines as delimiters
 * @param string $text
 * @return string $html
 */
function html_newlines_to_p($text)
{
    $text = clean_newlines($text);
    $html = explode("\n", $text);
    $html = array_map(function ($string) {
        return empty($string) ? '&nbsp;' : $string;
    }, $html);
    return '<p>' . implode('</p><p>', $html) . '</p>';
}
示例#2
0
function clean_html($html)
{
    $clean = "";
    $pre = 0;
    $html = clean_unicode($html);
    for ($i = 0; $i < mb_strlen($html); $i++) {
        //$c = substr($html, $i, 1);
        $c = mb_substr($html, $i, 1);
        if ($c == "<") {
            $s = "";
            for ($i = $i + 1; $i < mb_strlen($html); $i++) {
                //$c = substr($html, $i, 1);
                $c = mb_substr($html, $i, 1);
                if ($c == ">") {
                    break;
                }
                $s .= $c;
            }
            $tag = clean_tag($s);
            if ($tag == "<pre>") {
                $pre++;
            } else {
                if ($tag == "</pre>") {
                    $pre--;
                }
            }
            $clean .= $tag;
        } else {
            //if ($pre > 0 && $c == "\n") {
            //	$clean .= "<br/>";
            //} else {
            $clean .= $c;
            //}
        }
    }
    $clean = str_replace("\t", " ", $clean);
    $clean = str_replace("\n", " ", $clean);
    $clean = str_replace("\r", " ", $clean);
    while (string_has($clean, "  ")) {
        $clean = str_replace("  ", " ", $clean);
    }
    $clean = str_replace("> ", ">", $clean);
    $clean = str_replace(" <", "<", $clean);
    $clean = str_replace("FORCEWHITESPACE", " ", $clean);
    $clean = trim($clean);
    $clean = str_replace_all("  ", " ", $clean);
    $clean = str_replace_all("<br/><br/><br/>", "<br/><br/>", $clean);
    //	print "clean [$clean]";
    //	$clean = str_replace("<pre><br/>", "<pre>", $clean);
    //	$clean = str_replace("<br/></pre>", "</pre>", $clean);
    //	$clean = str_replace("<li><br/>", "<li>", $clean);
    //	$clean = str_replace("<br/></li>", "</li>", $clean);
    //	$clean = str_replace("<ul><br/>", "<ul>", $clean);
    //	$clean = str_replace("<br/></ul>", "</ul>", $clean);
    //	$clean = str_replace("<ol><br/>", "<ol>", $clean);
    //	$clean = str_replace("<br/></ol>", "</ol>", $clean);
    //	print "clean2 [$clean]";
    $clean = clean_newlines("pre", $clean);
    $clean = clean_newlines("ol", $clean);
    $clean = clean_newlines("ul", $clean);
    $clean = clean_newlines("li", $clean);
    $clean = clean_newlines("blockquote", $clean);
    $clean = clean_entities($clean);
    $clean = make_clickable($clean);
    return $clean;
}
示例#3
0
function clean_html($output)
{
    //Set wanted indentation
    $indent = str_repeat(" ", 4);
    //Uses previous function to seperate tags
    $output = clean_newlines($output);
    $output = explode("\n", $output);
    //Sets no indentation
    $indent_level = 0;
    foreach ($output as $key => $value) {
        //Removes all indentation
        $value = preg_replace("/\t+/", "", $value);
        $value = preg_replace("/^\\s+/", "", $value);
        $indent_replace = "";
        //Sets the indentation from current indent level
        for ($o = 0; $o < $indent_level; $o++) {
            $indent_replace .= $indent;
        }
        if (preg_match("/<(.+)\\/>/", $value)) {
            // If self-closing tag, simply apply indent
            $output[$key] = $indent_replace . $value;
        } elseif (preg_match("/<!(.*)>/", $value)) {
            // If doctype declaration, simply apply indent
            $output[$key] = $indent_replace . $value;
        } elseif (preg_match("/<[^\\/](.*)>/", $value) and preg_match("/<\\/(.*)>/", $value)) {
            // If opening AND closing tag on same line, simply apply indent
            $output[$key] = $indent_replace . $value;
        } elseif (preg_match("/<\\/(.*)>/", $value) or preg_match("/^(\\s|\t)*\\}{1}(\\s|\t)*\$/", $value)) {
            // If closing HTML tag or closing JavaScript clams, decrease indentation and then apply the new level
            $indent_level--;
            $indent_replace = "";
            for ($o = 0; $o < $indent_level; $o++) {
                $indent_replace .= $indent;
            }
            $output[$key] = $indent_replace . $value;
        } elseif (preg_match("/<[^\\/](.*)>/", $value) and !preg_match("/<(link|meta|base|br|img|hr)(.*)>/", $value) or preg_match("/^(\\s|\t)*\\{{1}(\\s|\t)*\$/", $value)) {
            // If opening HTML tag AND not a stand-alone tag, or opening JavaScript clams, increase indentation and then apply new level
            $output[$key] = $indent_replace . $value;
            $indent_level++;
            $indent_replace = "";
            for ($o = 0; $o < $indent_level; $o++) {
                $indent_replace .= $indent;
            }
        } else {
            // Else only apply indentation
            $output[$key] = $indent_replace . $value;
        }
    }
    // Return single string seperated by newline
    return implode("\n", $output);
}
示例#4
0
function clean_html($output)
{
    // Set wanted indentation
    $indent = str_repeat(" ", 4);
    // Uses previous function to seperate tags
    $output = clean_newlines($output);
    $output = explode("\n", $output);
    // Sets no indentation
    $indent_level = 0;
    $skip = FALSE;
    foreach ($output as $key => $value) {
        // #modified
        if ($skip) {
            continue;
        }
        if (preg_match('/<(textarea|pre)>/', $value)) {
            /*_log('ch_clean_newlines');*/
            $skip = true;
            continue;
        }
        // start of tag
        if (preg_match('/<\\/(textarea|pre)>/', $value)) {
            /*_log('ch_clean_newlines');*/
            $skip = false;
            continue;
        }
        // end of tag
        // Removes all indentation
        $value = preg_replace("/\t+/", "", $value);
        $value = preg_replace("/^\\s+/", "", $value);
        $indent_replace = "";
        // Sets the indentation from current indent level
        for ($o = 0; $o < $indent_level; $o++) {
            $indent_replace .= $indent;
        }
        // If self-closing tag, simply apply indent
        if (preg_match("/<(.+)\\/>/", $value)) {
            $output[$key] = $indent_replace . $value;
        } elseif (preg_match("/<!(.*)>/", $value)) {
            $output[$key] = $indent_replace . $value;
        } elseif (preg_match("/<[^\\/](.*)>/", $value) and preg_match("/<\\/(.*)>/", $value)) {
            $output[$key] = $indent_replace . $value;
        } elseif (preg_match("/<\\/(.*)>/", $value) or preg_match("/^(\\s|\t)*\\}{1}(\\s|\t)*\$/", $value)) {
            $indent_level--;
            $indent_replace = "";
            for ($o = 0; $o < $indent_level; $o++) {
                $indent_replace .= $indent;
            }
            $output[$key] = $indent_replace . $value;
        } elseif (preg_match("/<[^\\/](.*)>/", $value) and !preg_match("/<(link|meta|base|br|img|hr)(.*)>/", $value) or preg_match("/^(\\s|\t)*\\{{1}(\\s|\t)*\$/", $value)) {
            $output[$key] = $indent_replace . $value;
            $indent_level++;
            $indent_replace = "";
            for ($o = 0; $o < $indent_level; $o++) {
                $indent_replace .= $indent;
            }
        } else {
            $output[$key] = $indent_replace . $value;
        }
    }
    // Return single string seperated by newline
    $output[] = '<!-- Thanks for reviewing the source. This code has been automatically re-indented. -->';
    $output = implode("\n", $output);
    return $output;
}
示例#5
0
 public function setBodyAttribute($body)
 {
     $this->attributes['body'] = clean_newlines($body);
 }