Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [klasa] bbcode
dyktek
post 10.05.2006, 13:53:46
Post #1





Grupa: Zarejestrowani
Postów: 240
Pomógł: 0
Dołączył: 18.01.2004
Skąd: rzeszów / kraków

Ostrzeżenie: (0%)
-----


Witam, podaje do ocenki smile.gif klase bbcode
metoda findImage jeszcze nie działa a reszta to:

Kod
[b]hadzia[/b]
[i]hadzia[/i]
[u]hadzia[/u]
[url:http://adres.pl?hadzia=cos:anchor]



proszę o wyrozumiałość dopiero wychodzę z programowania strukturalnego

  1. <?php
  2. /*
  3. * @ Obsluga BBcode
  4. * @ 09.05.06
  5. * @ hadzia...
  6. */
  7.  
  8. class bbCode
  9. {
  10. /* tekst do przeszukania */
  11. var $text;
  12.  
  13. /* podmiana bold
  14. * @ hadzia 
  15. */
  16. var $regBold  = '/\[()\](.*)\[\/([b])\]/';
  17.  
  18. /* podmiana italic
  19. * @ hadzia 
  20. */
  21. var $regItalic = '/\[()\](.*)\[\/([i])\]/';
  22.  
  23. /* podmiana underline
  24. * @ hadzia 
  25. */
  26. var $regUnderline = '/\[()\](.*)\[\/([u])\]/';
  27.  
  28. /* podmiana na linki 
  29. * @ http://www.hadzia.pl/adres.roz?=zm
  30. * @ [url:http://adres.pl:anchor]
  31. */
  32. var $regUrl = '/\[(url{1}):(http[s]?:\/\/{1}[\w\.-]+\.\w{2,6}.*?):([^:]+)\]/';
  33.  
  34. /* podmiana na img
  35. * @ [img:X]
  36. * @ X - int
  37. */
  38. var $regImage = '/\[(img):(\\d+)\]/';
  39.  
  40.  
  41. function setString( $text ){
  42. return $this->text = $text;
  43. }
  44.  
  45. function changeCode(){
  46. return $this->findUrl( $this->findUnderline( $this->findItalic( $this->findBold( $this->text ) ) ) );
  47. }
  48.  
  49. function findBold( $sBold ){
  50. if( preg_match ( $this->regBold, $sBold ) ){
  51. return str_replace( "", "</b>", str_replace( "[b]", "<b>", $sBold) );
  52. } else {
  53. return $sBold;
  54. }
  55. }
  56.  
  57. function findItalic( $sItalic ){
  58. if( preg_match ( $this->regItalic, $sItalic ) ){
  59. return str_replace("", "</i>", str_replace( "[i]", "<i>", $sItalic ) );
  60. } else {
  61. return $sItalic;
  62. }
  63. }
  64.  
  65. function findUnderline( $sUnderline ){
  66. if( preg_match ( $this->regUnderline, $sUnderline ) ){
  67. return str_replace( "", "</u>", str_replace( "[u]", "<u>", $sUnderline ) );
  68. } else {
  69. return $sUnderline;
  70. }
  71. }
  72.  
  73. function findUrl( $sUrl ){
  74. if ( preg_match_all ( $this->regUrl, $sUrl, $matches, PREG_SET_ORDER ) ){
  75. for($i = 0; $i < count($matches); $i++){
  76. $all  = $matches[$i][0];
  77. $url  = $matches[$i][2];
  78. $anchor = $matches[$i][3];
  79. $sUrl = str_replace( $all,'<a href="'. $url .'">'. $anchor .'</a>',$sUrl );
  80. }
  81. return $sUrl;
  82. } else {
  83. return $sUrl;
  84. }
  85. }
  86.  
  87. /*
  88. function findImage(){
  89. if ( preg_match_all ( $this->regImage, $this->text, $matches, PREG_SET_ORDER ) ){
  90. echo'<pre>';
  91. print_r($matches);
  92. echo'</pre>';
  93.  
  94. } else {
  95. return $this->text;
  96. }
  97.  
  98. }
  99. */
  100. }
  101.  
  102. $bbCode = new bbCode;
  103.  
  104. $bbCode->setString( $_GET['text'] );
  105. echo $bbCode->changeCode();
  106. ?>


Ten post edytował dyktek 10.05.2006, 13:54:36
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 4)
sf
post 10.05.2006, 17:53:49
Post #2





Grupa: Zarejestrowani
Postów: 1 597
Pomógł: 30
Dołączył: 19.02.2003
Skąd: Tychy

Ostrzeżenie: (0%)
-----


b, i, u jest na jedno kopyto... po co tworzyć do tego 3 osobne funkcje? lepiej utworzyć jedną funkcję do tego...


--------------------
Zapraszam na mój php blog, tworzenie stron.
Go to the top of the page
+Quote Post
mike
post 10.05.2006, 19:31:10
Post #3





Grupa: Przyjaciele php.pl
Postów: 7 494
Pomógł: 302
Dołączył: 31.03.2004

Ostrzeżenie: (0%)
-----


A widziałeś kiedyś takie coś:
  1. <?php
  2.  
  3. $arrInsert = array( '', ');
  4. $arrReplace = array( '<b>', '</b>' );
  5. str_replace( $arrInsert, $arrReplace, $sBold) );
  6.  
  7. ?>

:?:
Go to the top of the page
+Quote Post
strife
post 10.05.2006, 20:37:07
Post #4





Grupa: Przyjaciele php.pl
Postów: 2 605
Pomógł: 96
Dołączył: 22.10.2004
Skąd: UK

Ostrzeżenie: (0%)
-----


Polecam abyś zapoznał się z klasą, może wpadniesz na pomysł jak swoją udoskonalić bo jest ona do napisania w 10 minut i nic nadzwczajnego w niej nie widzę winksmiley.jpg


--------------------
Go to the top of the page
+Quote Post
dyktek
post 10.05.2006, 21:21:57
Post #5





Grupa: Zarejestrowani
Postów: 240
Pomógł: 0
Dołączył: 18.01.2004
Skąd: rzeszów / kraków

Ostrzeżenie: (0%)
-----


Zastosowałem się do kilku wskazówek, wynik poniżej winksmiley.jpg

  1. <?php
  2. /*
  3. * @ Obsluga BBcode
  4. * @ 09.05.06
  5. * @ hadzia...
  6. */
  7.  
  8. class bbCode
  9. {
  10. /* tekst do przeszukania */
  11. var $text;
  12.  
  13. /* podmiana bold
  14. * @ hadzia 
  15. */
  16. var $regBold  = '/\[()\](.*)\[\/([b])\]/';
  17.  
  18. /* podmiana italic
  19.  * @ hadzia 
  20.  */
  21. var $regItalic = '/\[()\](.*)\[\/([i])\]/';
  22.  
  23. /* podmiana underline
  24.  * @ hadzia 
  25.  */
  26. var $regUnderline = '/\[()\](.*)\[\/([u])\]/';
  27.  
  28. /* podmiana na linki 
  29.  * @ http://www.hadzia.pl/adres.roz?=zm
  30.  * @ [url:http://adres.pl:anchor]
  31.  */
  32. var $regUrl = '/\[(url{1}):(http[s]?:\/\/{1}[\w\.-]+\.\w{2,6}.*?):([^:]+)\]/';
  33.  
  34. /* podmiana na img
  35. * @ [img:X]
  36. * @ X - int
  37. */
  38. var $regImage = '/\[(img):(\\d+)\]/';
  39.  
  40. /* tablica z wyrazeniami regularnymi [b], [i], [u] */
  41. var $arrReg = array();
  42.  
  43. /* konfiguracja tagow */
  44.  
  45. var $bbCode = array( '0' => array('[b]', ''),
  46.  '1' => array('[i]', ''),
  47.  '2' => array('[u]', '') );
  48.  
  49. var $bbCodeR = array( '0' => array('<b>', '</b>'),
  50. '1' => array('<i>', '</i>'),
  51. '2' => array('<u>', '</u>') );
  52.  
  53.  
  54. function setString( $text ){
  55. return $this->text = $text;
  56. }
  57.  
  58. function showResult(){
  59. return $this->findReplace( $this->text );
  60. }
  61.  
  62. function setArr(){
  63. return $this->arrReg = array( $this->regBold, $this->regItalic, $this->regUnderline );
  64. }
  65.  
  66. function findReplace($sChange){
  67. $this->setArr();
  68. $a = 0;
  69. for($i = 0; $i < count($this->arrReg); $i++){
  70. if($a == 0) $sSor = $sChange; else $sSor = $sResult;
  71. if( preg_match ( $this->arrReg[$i], $sSor ) ){
  72. switch($i){
  73. case 0 : $sResult = str_replace( $this->bbCode[$i][0], $this->bbCodeR[$i][0], str_replace( $this->bbCode[$i][1], $this->bbCodeR[$i][1], $sSor) ); $a++; break;
  74. case 1 : $sResult = str_replace( $this->bbCode[$i][0], $this->bbCodeR[$i][0], str_replace( $this->bbCode[$i][1], $this->bbCodeR[$i][1], $sSor) ); $a++; break;
  75. case 2 : $sResult = str_replace( $this->bbCode[$i][0], $this->bbCodeR[$i][0], str_replace( $this->bbCode[$i][1], $this->bbCodeR[$i][1], $sSor) ); $a++; break;
  76. }
  77. }
  78. }
  79.  return $this->findUrl( $sResult );
  80. }
  81.  
  82. function findUrl( $sUrl ){
  83. if ( preg_match_all ( $this->regUrl, $sUrl, $matches, PREG_SET_ORDER ) ){
  84. for($i = 0; $i < count($matches); $i++){
  85. $all  = $matches[$i][0];
  86. $url  = $matches[$i][2];
  87. $anchor = $matches[$i][3];
  88. $sUrl = str_replace( $all,'<a href="'. $url .'">'. $anchor .'</a>',$sUrl );
  89. }
  90. return $sUrl;
  91. } else {
  92. return $sUrl;
  93. }
  94. }
  95.  
  96. }
  97.  
  98. $bbCode = new bbCode;
  99. $bbCode->setString( $_GET['text'] );
  100. echo $bbCode->showResult();
  101.  
  102. ?>


Ten post edytował dyktek 11.05.2006, 10:13:14
Go to the top of the page
+Quote Post

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 Wersja Lo-Fi Aktualny czas: 31.07.2025 - 10:42