예제 #1
0
파일: tools.php 프로젝트: gitkv/bash
function TxtToHTML($str, $bMakeUrls = true, $iMaxStringLen = 0, $QUOTE_ENABLED = "N", $NOT_CONVERT_AMPERSAND = "Y", $CODE_ENABLED = "N", $BIU_ENABLED = "N", $quote_table_class = "quotetable", $quote_head_class = "tdquotehead", $quote_body_class = "tdquote", $code_table_class = "codetable", $code_head_class = "tdcodehead", $code_body_class = "tdcodebody", $code_textarea_class = "codetextarea", $link_class = "txttohtmllink", $arUrlEvent = array(), $link_target = "_self")
{
    global $QUOTE_ERROR, $QUOTE_OPENED, $QUOTE_CLOSED;
    $QUOTE_ERROR = $QUOTE_OPENED = $QUOTE_CLOSED = 0;
    $str = delete_special_symbols($str);
    // вставим спецсимвол chr(2) там где в дальнейшем необходимо вставить пробел
    if ($iMaxStringLen > 0) {
        $str = InsertSpaces($str, $iMaxStringLen, chr(2), true);
    }
    // \ => chr(8)
    $str = str_replace("\\", chr(8), $str);
    // спецсимвол замен¤ющий слэш "\"
    // <quote>...</quote> => [quote]...[/quote]
    if ($QUOTE_ENABLED == "Y") {
        $str = preg_replace("#(?:<|\\[)(/?)quote(.*?)(?:>|\\])#is", " [\\1quote]", $str);
    }
    // <code>...</code> => [code]...[/code]
    // \n => chr(4)
    // \r => chr(5)
    if ($CODE_ENABLED == "Y") {
        $helper = new CConvertorsPregReplaceHelper("");
        $str = preg_replace("#<code(\\s+[^>]*>|>)(.+?)</code(\\s+[^>]*>|>)#is", "[code]\\2[/code]", $str);
        $str = preg_replace_callback("#\\[code(\\s+[^\\]]*\\]|\\])(.+?)\\[/code(\\s+[^\\]]*\\]|\\])#is", array($helper, "convertCodeTagForHtmlBefore"), $str);
    }
    // <b>...</b> => [b]...[/b]
    // <i>...</i> => [i]...[/i]
    // <u>...</u> => [u]...[/u]
    if ($BIU_ENABLED == "Y") {
        $str = preg_replace("#<b(\\s+[^>]*>|>)(.+?)</b(\\s+[^>]*>|>)#is", "[b]\\2[/b]", $str);
        $str = preg_replace("#<i(\\s+[^>]*>|>)(.+?)</i(\\s+[^>]*>|>)#is", "[i]\\2[/i]", $str);
        $str = preg_replace("#<u(\\s+[^>]*>|>)(.+?)</u(\\s+[^>]*>|>)#is", "[u]\\2[/u]", $str);
    }
    // URL => chr(1).URL."/".chr(1)
    // EMail => chr(3).E-Mail.chr(3)
    if ($bMakeUrls) {
        //hide @ from next regexp with chr(11)
        $str = preg_replace_callback("#((http|https|ftp):\\/\\/[a-z:@,.'/\\#\\%=~\\&?*+\\[\\]_0-9--]+)#is", array("CConvertorsPregReplaceHelper", "extractUrl"), $str);
        $str = preg_replace("#(([=_\\.'0-9a-z+~--]+)@[_0-9a-z--.]+\\.[a-z]{2,10})#is", chr(3) . "\\1" . chr(3), $str);
        //replace back to @
        $str = str_replace(chr(11), '@', $str);
    }
    // конвертаци¤ критичных символов
    if ($NOT_CONVERT_AMPERSAND != "Y") {
        $str = str_replace("&", "&amp;", $str);
    }
    static $search = array("<", ">", "\"", "'", "%", ")", "(", "+");
    static $replace = array("&lt;", "&gt;", "&quot;", "&#39;", "&#37;", "&#41;", "&#40;", "&#43;");
    $str = str_replace($search, $replace, $str);
    // chr(1).URL."/".chr(1) => <a href="URL">URL</a>
    // chr(3).E-Mail.chr(3) => <a href="mailto:E-Mail">E-Mail</a>
    if ($bMakeUrls) {
        $script = $arUrlEvent["SCRIPT"];
        $helper = new CConvertorsPregReplaceHelper("");
        $helper->setLinkClass($link_class);
        $helper->setLinkTarget($link_target);
        $helper->setEvents($arUrlEvent["EVENT1"], $arUrlEvent["EVENT2"], $arUrlEvent["EVENT3"]);
        if (strlen($script)) {
            $helper->setScript($script);
        }
        $str = preg_replace_callback("#([^\n]+?)/#is", array($helper, "convertToHref"), $str);
        $str = preg_replace_callback("#([^\n]+?)#is", array($helper, "convertToMailTo"), $str);
    }
    $str = str_replace("\r\n", "\n", $str);
    $str = str_replace("\n", "<br />\n", $str);
    $str = preg_replace("# {2}#", "&nbsp;&nbsp;", $str);
    $str = preg_replace("#\t#", "&nbsp;&nbsp;&nbsp;&nbsp;", $str);
    // chr(2) => " "
    if ($iMaxStringLen > 0) {
        $str = str_replace(chr(2), "<wbr>", $str);
    }
    // [quote]...[/quote] => <table>...</table>
    if ($QUOTE_ENABLED == "Y") {
        $helper = new CConvertorsPregReplaceHelper("");
        $helper->setQuoteClasses($quote_table_class, $quote_head_class, $quote_body_class);
        $str = preg_replace_callback("#(\\[quote(.*?)\\](.*)\\[/quote(.*?)\\])#is", array($helper, "convertQuoteTag"), $str);
    }
    // [code]...[/code] => <textarea>...</textarea>
    // chr(4) => \n
    // chr(5) => \r
    if ($CODE_ENABLED == "Y") {
        $helper = new CConvertorsPregReplaceHelper("");
        $helper->setCodeClasses($code_table_class, $code_head_class, $code_body_class, $code_textarea_class);
        $str = preg_replace_callback("#\\[code\\](.*?)\\[/code\\]#is", array($helper, "convertCodeTagForHtmlAfter"), $str);
        $str = str_replace(chr(4), "\n", $str);
        $str = str_replace(chr(5), "\r", $str);
        $str = str_replace(chr(6), " ", $str);
        $str = str_replace(chr(7), "\t", $str);
        $str = str_replace(chr(16), "[", $str);
        $str = str_replace(chr(17), "]", $str);
    }
    // [b]...[/b] => <b>...</b>
    // [i]...[/i] => <i>...</i>
    // [u]...[/u] => <u>...</u>
    if ($BIU_ENABLED == "Y") {
        $str = preg_replace("#\\[b\\](.*?)\\[/b\\]#is", "<b>\\1</b>", $str);
        $str = preg_replace("#\\[i\\](.*?)\\[/i\\]#is", "<i>\\1</i>", $str);
        $str = preg_replace("#\\[u\\](.*?)\\[/u\\]#is", "<u>\\1</u>", $str);
    }
    // chr(8) => \
    $str = str_replace(chr(8), "\\", $str);
    $str = delete_special_symbols($str);
    return $str;
}
예제 #2
0
파일: tools.php 프로젝트: ASDAFF/entask.ru
function TxtToHTML(
	$str,                                    // текст для преобразования
	$bMakeUrls             = true,           // true - преобразовавыть URL в <a href="URL">URL</a>
	$iMaxStringLen         = 0,              // максимальная длина фразы без пробелов или символов перевода каретки
	$QUOTE_ENABLED         = "N",            // Y - преобразовать <QUOTE>...</QUOTE> в рамку цитаты
	$NOT_CONVERT_AMPERSAND = "Y",            // Y - не преобразовывать символ "&" в "&amp;"
	$CODE_ENABLED          = "N",            // Y - преобразовать <CODE>...</CODE> в readonly textarea
	$BIU_ENABLED           = "N",            // Y - преобразовать <B>...</B> и т.д. в соответствующие HTML тэги
	$quote_table_class     = "quotetable",   // css класс на таблицу цитаты
	$quote_head_class      = "tdquotehead",  // css класс на первую TD таблицы цитаты
	$quote_body_class      = "tdquote",      // css класс на вторую TD таблицы цитаты
	$code_table_class      = "codetable",    // css класс на таблицу кода
	$code_head_class       = "tdcodehead",   // css класс на первую TD таблицы кода
	$code_body_class       = "tdcodebody",   // css класс на вторую TD таблицы кода
	$code_textarea_class   = "codetextarea", // css класс на textarea в таблице кода
	$link_class            = "txttohtmllink",// css класс на ссылках
	$arUrlEvent            = array(),        // массив в нем если заданы ключи EVENT1, EVENT2, EVENT3 то ссылки будут через $arUrlEvent["SCRIPT"] (по умолчанию равен "/bitrix/redirect.php")
	$link_target           = "_self"         // tagret открытия страницы
)
{
	global $QUOTE_ERROR, $QUOTE_OPENED, $QUOTE_CLOSED;
	$QUOTE_ERROR = $QUOTE_OPENED = $QUOTE_CLOSED = 0;

	$str = delete_special_symbols($str);

	// вставим спецсимвол chr(2) там где в дальнейшем необходимо вставить пробел
	if($iMaxStringLen>0)
		$str = InsertSpaces($str, $iMaxStringLen, chr(2), true);

	// \ => chr(8)
	$str = str_replace("\\", chr(8), $str); // спецсимвол заменяющий слэш "\"

	// <quote>...</quote> => [quote]...[/quote]
	if ($QUOTE_ENABLED=="Y")
		$str = preg_replace("#(?:<|\\[)(/?)quote(.*?)(?:>|\\])#is", " [\\1quote]", $str);

	// <code>...</code> => [code]...[/code]
	// \n => chr(4)
	// \r => chr(5)
	if ($CODE_ENABLED=="Y")
	{
		$helper = new CConvertorsPregReplaceHelper("");
		$str = preg_replace("#<code(\\s+[^>]*>|>)(.+?)</code(\\s+[^>]*>|>)#is", "[code]\\2[/code]", $str);
		$str = preg_replace_callback("#\\[code(\\s+[^\\]]*\\]|\\])(.+?)\\[/code(\\s+[^\\]]*\\]|\\])#is", array($helper, "convertCodeTagForHtmlBefore"), $str);
	}

	// <b>...</b> => [b]...[/b]
	// <i>...</i> => [i]...[/i]
	// <u>...</u> => [u]...[/u]
	if ($BIU_ENABLED=="Y")
	{
		$str = preg_replace("#<b(\\s+[^>]*>|>)(.+?)</b(\\s+[^>]*>|>)#is", "[b]\\2[/b]", $str);
		$str = preg_replace("#<i(\\s+[^>]*>|>)(.+?)</i(\\s+[^>]*>|>)#is", "[i]\\2[/i]", $str);
		$str = preg_replace("#<u(\\s+[^>]*>|>)(.+?)</u(\\s+[^>]*>|>)#is", "[u]\\2[/u]", $str);
	}

	// URL => chr(1).URL."/".chr(1)
	// EMail => chr(3).E-Mail.chr(3)
	if($bMakeUrls)
	{
		//hide @ from next regexp with chr(11)
		$str = preg_replace_callback("#((http|https|ftp):\\/\\/[a-z:@,.'/\\#\\%=~\\&?*+\\[\\]_0-9\x01-\x08-]+)#is", array("CConvertorsPregReplaceHelper", "extractUrl"), $str);
		$str = preg_replace("#(([=_\\.'0-9a-z+~\x01-\x08-]+)@[_0-9a-z\x01-\x08-.]+\\.[a-z]{2,10})#is", chr(3)."\\1".chr(3), $str);
		//replace back to @
		$str = str_replace(chr(11), '@', $str);
	}

	// конвертация критичных символов
	if ($NOT_CONVERT_AMPERSAND!="Y") $str = str_replace("&", "&amp;", $str);
	static $search=array("<",">","\"","'","%",")","(","+");
	static $replace=array("&lt;","&gt;","&quot;","&#39;","&#37;","&#41;","&#40;","&#43;");
	$str = str_replace($search, $replace, $str);

	// chr(1).URL."/".chr(1) => <a href="URL">URL</a>
	// chr(3).E-Mail.chr(3) => <a href="mailto:E-Mail">E-Mail</a>
	if($bMakeUrls)
	{
		$script = $arUrlEvent["SCRIPT"];
		$helper = new CConvertorsPregReplaceHelper("");
		$helper->setLinkClass($link_class);
		$helper->setLinkTarget($link_target);
		$helper->setEvents($arUrlEvent["EVENT1"], $arUrlEvent["EVENT2"], $arUrlEvent["EVENT3"]);
		if (strlen($script))
			$helper->setScript($script);
		$str = preg_replace_callback("#\x01([^\n\x01]+?)/\x01#is", array($helper, "convertToHref"), $str);
		$str = preg_replace_callback("#\x03([^\n\x03]+?)\x03#is", array($helper, "convertToMailTo"), $str);
	}

	$str = str_replace("\r\n", "\n", $str);
	$str = str_replace("\n", "<br />\n", $str);
	$str = preg_replace("# {2}#", "&nbsp;&nbsp;", $str);
	$str = preg_replace("#\t#", "&nbsp;&nbsp;&nbsp;&nbsp;", $str);

	// chr(2) => " "
	if($iMaxStringLen>0)
		$str = str_replace(chr(2), "<wbr>", $str);

	// [quote]...[/quote] => <table>...</table>
	if ($QUOTE_ENABLED=="Y")
	{
		$helper = new CConvertorsPregReplaceHelper("");
		$helper->setQuoteClasses($quote_table_class, $quote_head_class, $quote_body_class);
		$str = preg_replace_callback("#(\\[quote(.*?)\\](.*)\\[/quote(.*?)\\])#is", array($helper, "convertQuoteTag"), $str);
	}

	// [code]...[/code] => <textarea>...</textarea>
	// chr(4) => \n
	// chr(5) => \r
	if ($CODE_ENABLED=="Y")
	{
		$helper = new CConvertorsPregReplaceHelper("");
		$helper->setCodeClasses($code_table_class, $code_head_class, $code_body_class, $code_textarea_class);
		$str = preg_replace_callback("#\\[code\\](.*?)\\[/code\\]#is", array($helper, "convertCodeTagForHtmlAfter"), $str);
		$str = str_replace(chr(4), "\n", $str);
		$str = str_replace(chr(5), "\r", $str);
		$str = str_replace(chr(6), " ", $str);
		$str = str_replace(chr(7), "\t", $str);
		$str = str_replace(chr(16), "[", $str);
		$str = str_replace(chr(17), "]", $str);
	}

	// [b]...[/b] => <b>...</b>
	// [i]...[/i] => <i>...</i>
	// [u]...[/u] => <u>...</u>
	if ($BIU_ENABLED=="Y")
	{
		$str = preg_replace("#\\[b\\](.*?)\\[/b\\]#is", "<b>\\1</b>", $str);
		$str = preg_replace("#\\[i\\](.*?)\\[/i\\]#is", "<i>\\1</i>", $str);
		$str = preg_replace("#\\[u\\](.*?)\\[/u\\]#is", "<u>\\1</u>", $str);
	}

	// chr(8) => \
	$str = str_replace(chr(8), "\\", $str);

	$str = delete_special_symbols($str);

	return $str;
}