Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Wyróżnianie elementów tabeli w tekście
WEC_26
post
Post #1





Grupa: Zarejestrowani
Postów: 29
Pomógł: 0
Dołączył: 23.08.2006

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


Mam tabelę:
  1. <?php
  2. $tabela = array('elemrnt1', 'element2', 'element3');
  3. ?>

i tekst:
  1. <?php
  2. $tekst = 'Coś tam element1 i coś tam innego element3 no i tez jest element2. Do tego jest 
    element1 i element2'
    ;
  3. ?>


I chcę aby w $tekst były podkreślone wszystkie elementy $tabela...
Więc robię skrypt:
  1. <?php
  2. foreach($tabela as $word)
  3. {
  4. $Gotowy = str_replace(strtolower($word), '<b>'.$word.'</b>', strtolower($tekst));
  5. }
  6. echo $Gotowy;
  7. ?>


Nom i niestety mając 3 elementy tabeli pętla foreach powtarza się 3 razy, zastępując $Gotowy nowym ciągiem i w ostateczności $Gotowy wyrzuca tekścik z podkreślonym tylko ostatnim elementem tabeli tj. 'element3'.

Czy ktoś wie jak otrzymać tekst w podkreślonymi wszystkimi elementami tabeli?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
Ziels
post
Post #2





Grupa: Zarejestrowani
Postów: 235
Pomógł: 2
Dołączył: 30.06.2006

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


  1. <?php
  2. $text = '';
  3. foreach($words as $word){
  4. $text .= '<b>' . $word . '</b>';
  5. }
  6. echo $text;
  7. ?>
Go to the top of the page
+Quote Post
Sedziwoj
post
Post #3





Grupa: Zarejestrowani
Postów: 793
Pomógł: 32
Dołączył: 23.11.2006
Skąd: Warszawa

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


  1. <?php
  2. foreach($tabela as $word)
  3. {
  4. $tekst = str_replace($word, '<b>'.strtolower($word).'</b>', $tekst);
  5. }
  6. echo $tekst;
  7. ?>


Ale chyba tak lepiej (IMG:http://forum.php.pl/style_emoticons/default/biggrin.gif)
  1. <?php
  2. $tabela = array('elemrnt1', 'element2', 'element3');
  3. $tabela2 = array('<b>elemrnt1</b>', '<b>element2</b>', '<b>element3</b>');
  4. $tekst = 'Coś tam element1 i coś tam innego element3 no i tez jest element2. Do tego jest 
    element1 i element2'
    ;
  5.  
  6. $tekst = str_replace($tabela, $tabela2, strtolower($tekst));
  7.  
  8. echo $tekst;
  9. ?>


Ten post edytował Sedziwoj 10.12.2006, 17:53:18
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: 22.08.2025 - 16:44