Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> preg replace callback + zagniezdzony bbcode
deniol13
post 22.10.2010, 16:27:12
Post #1





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

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


witam, jak zastosowac preg replace callback aby dzialal zagniezdzony bbcode? mam np taki regexp i czesc kodu

  1. $regExp = '\[code\](.*)\[\/code\]';
  2. $text = preg_replace_callback( '/' . $regExp . '/siU', array( $this, 'codeParse' ), $text, 9999 );


a metoda codeParse wyglada tak

  1. private function codeParse( $text )
  2. {
  3. if( is_array( $text ) )
  4. {
  5. $text = '<div class="code">' . $text[1] . '</div>';
  6. }
  7.  
  8. return $text;
  9. }


jak jest pojedynczy code lub sa dwa ale oddzielne to jest ok, ale jak juz jeden dam w drugi to nie za bardzo (zagniezdzone)

podbijam
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
Spawnm
post 22.10.2010, 16:37:51
Post #2





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




Zobacz z takim regexem:
  1. #\<!--c1--><div class='codetop'>Kod</div><div class='codemain'><!--ec1-->((?:[^[]|\[(?!/?code])|(?R))+)\<!--c2--></div><!--ec2-->#
Powód edycji: [Spawnm]: #[code]((?:[^[]|[(?!/?code])|(?R))+)[/code]#
Go to the top of the page
+Quote Post
deniol13
post 22.10.2010, 16:54:15
Post #3





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

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


dzieki ale nie powiem czy dziala, sam rozwiazelem problem biggrin.gif (huraaaa, pol litrwa kawy z mlekiem + nerwy robi swoje)

jak by ktos mial podobny problem,
  1. private function codeParse( $matches )
  2. {
  3. if( is_array( $matches ) )
  4. {
  5. $replacments = 0;
  6.  
  7. $matches = $matches[0];
  8.  
  9. while( true )
  10. {
  11. $matches = preg_replace( '#\[code\](.*)\[/code\]#siU', '<div class="code_body"><div class="code_head">Kod</div><div class="code_text">$1</div></div>', $matches, 999, $replacments );
  12.  
  13. if( $replacments == 0 )
  14. {
  15. break;
  16. }
  17. }
  18.  
  19. return $matches;
  20. }
  21. else
  22. {
  23. return $matches[1];
  24. }
  25. }
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: 19.07.2025 - 13:09