Witam,
Jestem w trakcie pisania strony, na której zamieściłem BBCode. Są to dwie funkcje, gdzie jedna zmienia z HTML na BBCode, a druga z BBCode na HTML:

  1. <?php
  2. function bbcode_encode( $str )
  3. {
  4. $str = preg_replace( "#\<b\>(.*?)\</b\>#si",'\\1', $str );
  5. $str = preg_replace( "#\<i\>(.*?)\</i\>#si",'\\1', $str );
  6. $str = preg_replace( "#\<u\>(.*?)\</u\>#si",'\\1', $str );
  7. $str = preg_replace( "#\<s\>(.*?)\</s\>#si",'\\1', $str );
  8. $str = preg_replace( "#\<left\>(.*?)\</left\>#si",'
    \\1
    '
    , $str );
  9. $str = preg_replace( "#\<center\>(.*?)\</center\>#si",'
    \\1
    '
    , $str );
  10. $str = preg_replace( "#\<right\>(.*?)\</right\>#si",'
    \\1
    '
    , $str );
  11. $str = preg_replace( "#\<font size=\"(.*?)\"\>(.*?)\</font\>#si",'\2', $str );
  12. $str = preg_replace( "#\<font color=\"(.*?)\"\>(.*?)\</font\>#si",'\2', $str );
  13. $str = preg_replace( "#\<font color=\"(.*?)\" size=\"(.*?)\"\>(.*?)\</font\>#si",'[font color=\\1 size=\\2]\\3[/font]', $str );
  14. $str = preg_replace( "#\<font size=\"(.*?)\" color=\"(.*?)\"\>(.*?)\</font\>#si",'[font size=\\1 color=\\2]\\3[/font]', $str );
  15. $str = preg_replace( "#\<img src=\"(.*?)\" /\>#si",'[img]\\1[/img/]', $str );
  16. $str = preg_replace( "#\<a href=\"(.*?)\" target=\"_blank\">(.*?)\</a\>#si", "\\2", $str );
  17. $str = preg_replace( "#\<div style=\"(.*?)\"\>(.*?)\</div\>#si", '[div style=\\1]\\2[/div]', $str );
  18. $str = preg_replace( "#\<iframe width=\"640\" height=\"360\" src=\"http://www.youtube.com/embed/(.*?)\" frameborder=\"0\" allowfullscreen\>\</iframe\>#si",'[youtube]\\1[/youtube]',$str );
  19.  
  20. return $str;
  21. }
  22.  
  23. function bbcode_decode( $str )
  24. {
  25. $str = preg_replace( "#\[b\](.*?)\[/b\]#si",'<b>\\1</b>', $str );
  26. $str = preg_replace( "#\[i\](.*?)\[/i\]#si",'<i>\\1</i>', $str );
  27. $str = preg_replace( "#\[u\](.*?)\[/u\]#si",'<u>\\1</u>', $str );
  28. $str = preg_replace( "#\[s\](.*?)\[/s\]#si",'<s>\\1</s>', $str );
  29. $str = preg_replace( "#\[left\](.*?)\[/left\]#si",'<left>\\1</left>', $str );
  30. $str = preg_replace( "#\[center\](.*?)\[/center\]#si",'<center>\\1</center>', $str );
  31. $str = preg_replace( "#\[right\](.*?)\[/right\]#si",'<right>\\1</right>', $str );
  32. $str = preg_replace( "#\[font color=(.*?) size=(.*?)\](.*?)\[/font\]#si",'<font color="\\1" size="\\2">\\3</font>', $str );
  33. $str = preg_replace( "#\[font size=(.*?) color=(.*?)\](.*?)\[/font\]#si",'<font size="\\1" color="\\2">\\3</font>', $str );
  34. $str = preg_replace( "#\[size=(.*?)\](.*?)\[/size\]#si",'<font size="\\1">\\2</font>', $str );
  35. $str = preg_replace( "#\[color=(.*?)\](.*?)\[/color\]#si",'<font color="\\1">\\2</font>', $str );
  36. $str = preg_replace( "#\[img\](.*?)\[/img\]#si",'<img src="\\1" alt="" />', $str );
  37. $str = preg_replace( "#\[url=(http.*?)\](.*?)\[/url\]#si", "<a href=\"\\1\" target=\"_blank\">\\2</a>", $str );
  38. $str = preg_replace( "#\[url=(.*?)\](.*?)\[/url\]#si", "<a href=\"http://\\1\" target=\"_blank\">\\2</a>", $str );
  39. $str = preg_replace( "#\[div style=(.*?)\](.*?)\[/div\]#si",'<div style="\\1">\\2</div>', $str );
  40. $str = preg_replace( "#\[youtube\](.*?)\[/youtube\]#si",'<iframe width="640" height="360" src="http://www.youtube.com/embed/\\1" frameborder="0" allowfullscreen></iframe>', $str );
  41. return $str;
  42. }
  43. ?>


Domyślam się, że może nie wygląda to najlepiej i nie jest to najlepszy sposób na parsowanie tekstu, ale optymalizować kod będę później, na razie chcę doprowadzić to do stanu działania.

Problem polega na tym, że chciałbym dodać bbcode [file id=(jakaś liczba)]Tekst[/file]. W bazie danych mam tabelę `files`, która zawiera pola `ID` integer, auto increment, `name` varchar oraz `price` integer. W pole tekstowe, które następnie parsuje BBCode w HTML wpisuję dla przykładu [file id=5]Pobierz plik[/file]. Chciałbym żeby parser odczytał id 5, następnie pobrał z tabeli MySQL `files` pola `name` i `price`, gdzie `ID` = '5', po czym zmienił ten bbcode w pole form i umieścił w pola hidden "id", "name" i "price" odpowiednie wartości z MySQL.

Chciałbym prosić o pomoc lub nakierowanie, jak mogę coś takiego zrobić. Domyślam się, że preg_replace nie umożliwia pobrania `id`, dlatego jak można ten tekst sparsować w inny sposób?

Pozdrawiam.

############
@Edit
Problem został rozwiązany.

Rozwiązaniem jest wykorzystanie preg_match_all('/\[file(.*?)](.*?)\[\/file\]/msi', $str, $matches); a następnie wykorzystanie pętli foreach, a w niej preg_replace.

Można temat zamknąć, dziękuję.