Форум: Форум PHPФорум ApacheФорум Регулярные ВыраженияФорум MySQLHTML+CSS+JavaScriptФорум FlashРазное
Новые темы: 0000000
C++. Мастер-класс в задачах и примерах. Авторы: Кузнецов М.В., Симдянов И.В. Социальная инженерия и социальные хакеры. Авторы: Кузнецов М.В., Симдянов И.В. PHP на примерах (2 издание). Авторы: Кузнецов М.В., Симдянов И.В. Самоучитель PHP 5 / 6 (3 издание). Авторы: Кузнецов М.В., Симдянов И.В. Объектно-ориентированное программирование на PHP. Авторы: Кузнецов М.В., Симдянов И.В.
ВСЕ НАШИ КНИГИ
Консультационный центр SoftTime

Форум Регулярные Выражения

Выбрать другой форум

 

Здравствуйте, Посетитель!

вид форума:
Линейный форум Структурный форум

тема: MustHave: Универсальный парсер BB-кода (BBcode)
 
 автор: DDK   (20.01.2006 в 13:52)   письмо автору
 
 


<?php

   
function TMPL_Parser_BB$tmpl )
    {
     
$argv func_get_args();

     
$tmpl preg_replace"#\[b\](.+?)\[/b\]#is" "<b>\\1</b>" $tmpl );
     
$tmpl preg_replace"#\[i\](.+?)\[/i\]#is" "<i>\\1</i>" $tmpl );
     
$tmpl preg_replace"#\[u\](.+?)\[/u\]#is" "<u>\\1</u>" $tmpl );

     
$tmpl preg_replace"#\[center\](.+?)\[/center\]#is" "<div align='center'>\\1</div>" $tmpl );
     
$tmpl preg_replace"#\[right\](.+?)\[/right\]#is"   "<div align='right'>\\1</div>"  $tmpl );
     
$tmpl preg_replace"#\[left\](.+?)\[/left\]#is"     "<div align='left'>\\1</div>"   $tmpl );

     
$tmpl preg_replace"#<div align='(center|right|left)'>(.+?)</div>[^</p>]#is""<div align='\\1'>\\2</div><br>" $tmpl );

     
$tmpl preg_replace"#\(c\)#i"  "&copy;" $tmpl );
     
$tmpl preg_replace"#\(tm\)#i" "(tm)" $tmpl );
     
$tmpl preg_replace"#\(r\)#i"  "&reg;"  $tmpl );

     while ( 
preg_match"#\[font=([^\]]+)\](.*?)\[/font\]#ies"$tmpl ) )
      {  
$tmpl preg_replace"#\[font=([^\]]+)\](.*?)\[/font\]#is"    "\$this->regex_font_attr(array('s'=>'font','1'=>'\\1','2'=>'\\2'))"$txt );  }

     while( 
preg_match"#\[color=([^\]]+)\](.+?)\[/color\]#ies"$tmpl ) )
      {  
$tmpl preg_replace"#\[color=([^\]]+)\](.+?)\[/color\]#is" "<span style='color:\\1'>\\2</span>"       $tmpl );  }

     if( isset( 
$argv[2] ) && is_array$argv[2] ) )
      {
       
$tmpl preg_replace"#\[img=(\d)\]#ie" ,
                               
"\$this -> TMPL_Parser_BB_Image( \"&#092;\1\" , \"\" , \$argv[1] , \$argv[2] )" ,
                               
$tmpl );
       
$tmpl preg_replace"#\[img=(\d);align=(\w+)\]#ie" ,
                               
"\$this -> TMPL_Parser_BB_Image( \"&#092;\1\" , \"\\2\" , \$argv[1] , \$argv[2] )" ,
                               
$tmpl );
      }

     
$tmpl preg_replace"#\[img\](.+?)\[/img\]#ie" "<img src='\\1'>" $tmpl );

     
$tmpl preg_replace"#\[url\](.+?)\[/url\]#ie" ,
                             
"\$this -> TMPL_Parser_BB_link( \"&#092;\1\" , \"\" )" ,
                             
$tmpl );
     
$tmpl preg_replace"#\[url\s*=\s*(\S+?)\s*\](.*?)\[\/url\]#ie" ,
                             
"\$this -> TMPL_Parser_BB_link( \"&#092;\1\" , \"\\2\" )" ,
                             
$tmpl );

     
$tmpl preg_replace"#\n?\[list\](.+?)\[/list\]\n?#ies" ,
                             
"\$this -> TMPL_Parser_BB_CODE_List( \"&#092;\1\" , \"\" )" ,
                             
$tmpl );
     
$tmpl preg_replace"#\n?\[list=(a|A|i|I|1)\](.+?)\[/list\]\n?#ies" ,
                             
"\$this -> TMPL_Parser_BB_CODE_List( \"&#092;\2\" , \"\\1\" )" ,
                             
$tmpl );

     
$tmpl str_replace"<p></ul>"  "</ul><p>"      $tmpl );
     
$tmpl str_replace"<p><li>"   "<li>"          $tmpl );
     
$tmpl str_replace"</ul><li>" "</ul><p><li>"  $tmpl );

     
$tmpl preg_replace_callback"#\[html\](.+?)\[/html\]#is", array( $this "TMPL_Parser_BB_CODE_HTML" ) , $tmpl );

     
$tmpl $this -> TMPL_Parser_BB_Remove$tmpl );

     return 
$tmpl;
    }

   function 
TMPL_Parser_BB_Remove$tmpl )
    {
     
$tmpl preg_replace"#\[b\]|\[/b\]#is"                          "" $tmpl );
     
$tmpl preg_replace"#\[i\]|\[/i\]#is"                          "" $tmpl );
     
$tmpl preg_replace"#\[u\]|\[/u\]#is"                          "" $tmpl );
     
$tmpl preg_replace"#\[center\]|\[/center\]#is"                "" $tmpl );
     
$tmpl preg_replace"#\[right\]|\[/right\]#is"                  "" $tmpl );
     
$tmpl preg_replace"#\[left\]|\[/left\]#is"                    "" $tmpl );
     
$tmpl preg_replace"#\[font\s*=\s*(\S+?)\s*\]\[\/font\]#is"    "" $tmpl );
     
$tmpl preg_replace"#\[color\s*=\s*(\S+?)\s*\]|\[\/color\]#is" "" $tmpl );
     
$tmpl preg_replace"#\[img\]|\[/img\]#is"                      "" $tmpl );
     
$tmpl preg_replace"#\[img=\d\]#is"                            "" $tmpl );
     
$tmpl preg_replace"#\[img=\d;align=(.*?)\]#is"                "" $tmpl );
     
$tmpl preg_replace"#\[url\]|\[/url\]#is"                      "" $tmpl );
     
$tmpl preg_replace"#\[url\s*=\s*(\S+?)\s*\]#is"               "" $tmpl );
     
$tmpl preg_replace"#\[html\]|\[/html\]#is"                    "" $tmpl );

     return 
$tmpl;
    }

   function 
TMPL_Parser_BB_Image$number $align $src $images = array() )
    {
     
$align = ( empty( $align ) ) ? "align='left'" "align='" $align "'" ;

     if( isset( 
$images$number ] ) )
      {  return 
"<img src='images/" $src $images$number ] . "' border='0' "$align .">";  }
     else
      {  return 
"[Image Not Found]";  }
    }

   function 
TMPL_Parser_BB_link$href $discription )
    {
     
$style "content";

     if( empty( 
$discription ) )
      {
       
$discription = ( strlen$discription ) < 80 ) ? $href substr$href 80 ) . "..." ;
       return 
"<a href='" $href "' class='" $style "'>" $discription "</a>";
      }
     else
      {  return 
"<a href='" $href "' class='" $style "'>" $discription "</a>";  }
    }

   function 
TMPL_Parser_BB_CODE_List$tmpl $type )
    {
     switch( 
$type )
      {
       case 
"1"$list_type "1"; break;
       case 
"A"$list_type "A"; break;
       case 
"a"$list_type "a"; break;
       case 
"I"$list_type "I"; break;
       case 
"i"$list_type "i"; break;
       default:  
$list_type "1"; break;
      }

     
$tmpl preg_replace"#\[\*\]#" "<li>" $tmpl );
     
$tmpl "<ul type='" $list_type "'>" $tmpl "</ul>";

     return 
$tmpl;
    }

print 
TMPL_Parser_BB$text );
// Для замены каринок необходимо передать:
// вторым параметром: путь от корня до папки с картинками
// третьим - имена файлов самих картинок (массив)

?>

   
 
 автор: Crux   (20.01.2006 в 19:53)   письмо автору
 
   для: DDK   (20.01.2006 в 13:52)
 

что-то работь не хочет...
откуда взял? дай ссылку.

   
 
 автор: DDK   (22.01.2006 в 22:54)   письмо автору
 
   для: Crux   (20.01.2006 в 19:53)
 

Взял не помню откуда, яндексом нашёл. Работает это, мной лично проверенно. Но функцию я эту опубликовал скорее не для того, что бы её пользоваться, а для того, что бы из неё выдирать регулярные выражения тем, кто их не умеет писать сам... мне очень помогло.

   
Rambler's Top100
вверх

Rambler's Top100 Яндекс.Метрика Яндекс цитирования