Text_Highlighter::factory PHP Method

factory() public static method

Create a new Highlighter object for specified language
public static factory ( string $lang, array $options = [] ) : mixed
$lang string language, for example "SQL"
$options array Rendering options. This parameter is only keeped for BC reasons, use {@link Text_Highlighter::setRenderer()} instead
return mixed a newly created Highlighter object, or a PEAR error object on error
    public static function factory($lang, $options = array())
    {
        $lang = strtoupper($lang);
        $langFile = dirname(__FILE__) . "/Highlighter/{$lang}.php";
        if (is_file($langFile)) {
            include_once $langFile;
        } else {
            return false;
        }
        $classname = 'Text_Highlighter_' . $lang;
        if (!class_exists($classname)) {
            return false;
        }
        return new $classname($options);
    }

Usage Example

コード例 #1
0
ファイル: Testing.php プロジェクト: eugenzor/zfhrtool
 /**
  * Функция перенесена из функции addElementsForm() из-за возникающей ошибки:
  *     Cannot redeclare highlighter() (previously declared in 
  *     application\models\Form\Test\Testing.php:41) in 
  *     application\models\Form\Test\Testing.php on line 41
  * @param unknown_type $text
  * @param unknown_type $classExistsTH
  */
 private function _highlighter($text, $classExistsTH = false)
 {
     $text = str_replace('[js]', '[javascript]', $text);
     $text = str_replace('[/js]', '[/javascript]', $text);
     $text = str_replace(array('[code lang="js"]', '[code lang=\'js\']'), '[code lang="javascript"]', $text);
     $tags = '(?:php)|(?:sql)|(?:css)|(?:javascript)|(?:html)|(?:sh)';
     if ($classExistsTH) {
         $text .= '[code lang=""][/code]';
         // заменяет все теги на [code lang="langName"] и [/code]
         $text = preg_replace('/\\[(' . $tags . ')\\](.*?)\\[\\/(?:' . $tags . ')\\]/is', "[code lang='\$1']\$2[/code]", $text);
         //розбтвает строку и записывает в массив
         preg_match_all("/(.*?)\\[code lang=['\"](.*?)['\"]\\](.*?)\\[\\/code\\]/is", $text, $matches);
         $text = '';
         $count = count($matches[0]);
         for ($i = 0; $i < $count; $i++) {
             $text .= nl2br(htmlspecialchars($matches[1][$i]));
             if ($matches[2][$i]) {
                 $hl =& Text_Highlighter::factory($matches[2][$i]);
                 $text .= $hl->highlight($matches[3][$i]);
             }
         }
     } else {
         $text = htmlspecialchars($text);
         $text = preg_replace('/(\\[code lang=.*?\\])/is', '<pre>', $text);
         $text = str_replace(array('[php]', '[sql]', '[css]', '[js]', '[html]', '[bash]'), '<pre>', $text);
         $text = str_replace(array('[/php]', '[/sql]', '[/css]', '[/js]', '[/html]', '[/bash]', '[/code]'), '</pre>', $text);
         //$text = nl2br($text);
         $text = str_replace("\r\n", '<br/>', $text);
     }
     return $text;
 }
All Usage Examples Of Text_Highlighter::factory