Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [klasa] Prosty parser bbcode, Dla (nie)wymagających
deniol13
post
Post #1





Grupa: Zarejestrowani
Postów: 190
Pomógł: 2
Dołączył: 30.11.2009

Ostrzeżenie: (10%)
X----


Jest to mój pierwszy "parser" BBCode, dużo tu opisywać nie trzeba smile.gif

Kod

  1. <?php
  2. /*****************************************************************
  3.  * Fusion.Board
  4.  *
  5.  * @author Denis Wrobel
  6.  *****************************************************************/
  7.  
  8. class parser
  9. {
  10. private $text; //Text to parse
  11.  
  12. private $tags = array( 'bold' => array( 'regexp' => '\[b\](.*?)\[\/b\]',
  13. 'replace' => '<b>{VALUE_1}</b>',
  14. 'embed' => 0 ),
  15. 'uline'=> array( 'regexp' => '\[u\](.*?)\[\/u\]',
  16. 'replace' => '<u>{VALUE_1}</u>',
  17. 'embed' => 0 ),
  18. 'italic'=>array( 'regexp' => '\[i\](.*?)\[\/i\]',
  19. 'replace' => '<i>{VALUE_1}</i>',
  20. 'embed' => 0 ),
  21. 'url' => array( 'regexp' => '\[url\](.*?)\[\/url\]',
  22. 'replace' => '<a href="{VALUE_1}">{VALUE_1}</a>',
  23. 'embed' => 0 ),
  24. 'nurl' => array( 'regexp' => '\[url=(.*?)\](.*?)\[\/url\]',
  25. 'replace' => '<a href="{VALUE_1}">{VALUE_2}</a>',
  26. 'embed' => 0 ),
  27. 'sub' => array( 'regexp' => '\[sub\](.*?)\[\/sub\]',
  28. 'replace' => '<sub>{VALUE_1}</sub>',
  29. 'embed' => 0 ),
  30. 'sup' => array( 'regexp' => '\[sup\](.*?)\[\/sup\]',
  31. 'replace' => '<sup>{VALUE_1}</sup>',
  32. 'embed' => 0 ),
  33. 'center'=>array( 'regexp' => '\[center\](.*?)\[\/center\]',
  34. 'replace' => '<center>{VALUE_1}</center>',
  35. 'embed' => 0 ),
  36. 'image'=> array( 'regexp' => '\[img\](.*?)\[\/img\]',
  37. 'replace' => '<img src="{VALUE_1}" alt="IMAGE" title="IMAGE"/>',
  38. 'embed' => 0 ),
  39. 'strike'=>array( 'regexp' => '\[s\](.*?)\[\/s\]',
  40. 'replace' => '<s>{VALUE_1}</s>',
  41. 'embed' => 0 ),
  42. 'quote'=> array( 'regexp' => '\[quote\](.*)\[\/quote\]',
  43. 'replace' => '<div class="quote_body"><div class="quote_head">Cytat</div> <div class="quote_text">{VALUE_1}</div></div>',
  44. 'embed' => 1 ),
  45. 'uquote'=> array('regexp' => '\[quote=(.*)\](.*)\[\/quote\]',
  46. 'replace' => '<div class="quote_body"><div class="quote_head">Cytat ({VALUE_1})</div> <div class="quote_text">{VALUE_2}</div></div>',
  47. 'embed' => 1 ),
  48. 'code' => array( 'regexp' => '\[code\](.*?)\[\/code\]',
  49. 'replace' => '<div class="code_body"><div class="code_head">Kod</div> <div class="code_text">{VALUE_1}</div></div>',
  50. 'embed' => 1 ),
  51. 'color'=> array( 'regexp' => '\[color=(.*)\](.*?)\[\/color\]',
  52. 'replace' => '<span style="color: {VALUE_1}">{VALUE_2}</span>',
  53. 'embed' => 1 ),
  54. 'size' => array( 'regexp' => '\[size=(10|12|14|18|25)\](.*?)\[\/size\]',
  55. 'replace' => '<span style="font-size: {VALUE_1}">{VALUE_2}</span>',
  56. 'embed' => 1 ),
  57. );
  58.  
  59. public function parse( $text )
  60. {
  61. $this->text = $text;
  62.  
  63. $this->text = $this->prepare( $text );
  64.  
  65.  
  66. return $this->text;
  67. }
  68.  
  69. private function prepare( $text )
  70. {
  71. global $language;
  72.  
  73. if( strlen( $text ) )
  74. {
  75. if( count( $this->tags ) )
  76. {
  77. foreach( $this->tags AS $tag )
  78. {
  79. $tag['replace'] = preg_replace( '/\{VALUE_([0-9]{0,100})\}/si', '$$1', $tag['replace'] );
  80.  
  81. //Embed bbcode?
  82. if( $tag['embed'] == 1 )
  83. {
  84. $replacments = 0;
  85.  
  86. while( true )
  87. {
  88. $text = preg_replace( '/' . $tag['regexp'] . '/siU', $tag['replace'], $text, 999, $replacments );
  89.  
  90. if( $replacments == 0 )
  91. {
  92. break;
  93. }
  94. }
  95. }
  96. else
  97. {
  98. $text = preg_replace( '/' . $tag['regexp'] . '/si', $tag['replace'], $text );
  99. }
  100. }
  101. }
  102. }
  103.  
  104. return $text;
  105. }
  106. }
  107. ?>



CSS
  1. .quote_body
  2. {
  3. background-color: #e4e4e4;
  4. border: 1px solid #5475fa;
  5. padding: 1px;
  6. margin-top: 3px;
  7. }
  8.  
  9. .quote_head
  10. {
  11. background-color: #a0b4e7;
  12. color: #265793;
  13. font-weight: bold;
  14. padding: 5px;
  15. font-size: 13px;
  16. }
  17.  
  18. .quote_text
  19. {
  20. font-family: arial;
  21. font-size: 11px;
  22. padding: 3px;
  23. }
  24.  
  25. .code_body
  26. {
  27. background-color: #e7f9e3;
  28. border: 1px solid #1f9a25;
  29. padding: 1px;
  30. margin-top: 3px;
  31. }
  32.  
  33. .code_head
  34. {
  35. color: #67a217;
  36. font-weight: bold;
  37. padding: 5px;
  38. font-size: 13px;
  39. }
  40.  
  41. .code_text
  42. {
  43. font-family: arial;
  44. font-size: 11px;
  45. padding: 7px;
  46. }


Przepraszam za post pod postem, ale jak dam edytuj, mam dziwną treść wiadomości

Sposób użycia:



CODE
$parser = new parser;
echo $parser->parse('It works');


Licencja: :| róbta co chceta ale bez zmieniania autora w pliku
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 07:41