Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]highlight_string między znacznikami
kacpero1094
post
Post #1





Grupa: Zarejestrowani
Postów: 87
Pomógł: 0
Dołączył: 22.12.2008

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


Co zrobić, żeby funkcja hightlight_string, była używana tylko między znacznikami <code></code>?
Funkcja, która u mnie zamienia BBCode na HTML to:
  1. <?php
  2. function replace($text) {
  3. $text=htmlspecialchars($text);
  4. $text = nl2br($text);
  5. $text = preg_replace("#(.*?)#si",'<b>1</b>',$text);
  6. $text = preg_replace("#(.*?)#si",'<i>1</i>',$text);
  7. $text = preg_replace("#(.*?)#si",'<u>1</u>',$text);
  8. $text = preg_replace("#[img](.*?)[/img]#si",'<img src="1" alt="Kacper Kołodziej">',$text);
  9. $text = preg_replace("#[link=(http.*?)](.*?)[/link]#si",'<a target="_blank" href="1">2</a>',$text);
  10. $text = preg_replace("#[link=(.*?)](.*?)[/link]#si",'<a target="_blank" href="http://1">2</a>',$text);
  11. $text = preg_replace("#[url=(.*?)](.*?)[/link]#si",'<a target="_blank" href="http://1">2</a>',$text);
  12. $text = preg_replace("#(.*?)#si",'<a target="_blank" href="1">1</a>', $text);
  13. $text = preg_replace("#[link](.*?)#si",'<a target="_blank" href="http://1">1</a>', $text);
  14. $text = preg_replace("#[link](http.*?)[/url]#si",'<a target="_blank" href="1">1</a>', $text);
  15. $text = preg_replace("#[h2](.*?)[/h2]#si",'<h2>1</h2>',$text);
  16. $text = preg_replace("#[h1](.*?)[/h1]#si",'<h1>1</h1>',$text);
  17. $text = preg_replace("#[link](http.*?)[/link]#si",'<a href="1">1</a>',$text);
  18. $text = preg_replace("#[link](.*?)[/link]#si",'<a target="_blank" href="http://1">1</a>',$text);
  19. $text = preg_replace("#[code](.*?)[/code]#si",'<code>' . highlight_string('1') . '</code>',$text);
  20. }
  21. ?>


Ten post edytował kacpero1094 31.07.2009, 15:11:29
Go to the top of the page
+Quote Post
TrevorGryffits
post
Post #2





Grupa: Zarejestrowani
Postów: 139
Pomógł: 24
Dołączył: 29.07.2007
Skąd: Tarnowskie Góry

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


A moze preg_replace_callback?
Go to the top of the page
+Quote Post
Skie
post
Post #3





Grupa: Zarejestrowani
Postów: 555
Pomógł: 84
Dołączył: 20.02.2008
Skąd: Małopolska

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


Pobierz tekst z pomiędzy tych znaczników za pomocą preg_match_all() i wywołaj na tym wybraną przez Ciebie funkcję. Potem zamieniaj pierwotnie pobrany tekst na tekst "przeżuty" przez funkcję i podmień go używając preg_replace()

Ten post edytował Skie 31.07.2009, 20:53:13
Go to the top of the page
+Quote Post
kacpero1094
post
Post #4





Grupa: Zarejestrowani
Postów: 87
Pomógł: 0
Dołączył: 22.12.2008

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


można prosić o przykład?
nie bardzo rozumiem o co chodzi w funkcji preg_match_all()
Go to the top of the page
+Quote Post
Pawel_W
post
Post #5





Grupa: Zarejestrowani
Postów: 1 675
Pomógł: 286
Dołączył: 15.06.2009
Skąd: Wieliczka

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


preg_match_all to to samo co preg_match, tyle że preg_match zatrzymuje się przy pierwszym trafieniu a ~all szuka dalej (IMG:style_emoticons/default/winksmiley.jpg)

obie funkcje zwracają tablice w elementami pasującymi do wzorca
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 24.08.2025 - 04:17