Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php] zmiana ciagu znakow
webspy
post
Post #1





Grupa: Zarejestrowani
Postów: 186
Pomógł: 9
Dołączył: 3.03.2008

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


Wiem, że tych tematów jest dużo ale nie mogę znaleźć tego odpowiedniego dla mnie:

Kod
$post = str_replace(array(
                ':)',
                ':]',
                ':('    
        ), array(
                '<img src="smile.gif"/>',
                '<img src="eye.gif"/>',
                '<img src="sad.gif"/>'  
        ), $post);


Mam taki przykład kodu, i chcę aby zmieniało taki znak ":)" tak samo jak ciag znakow ":)))))))))))"
Wynikiem byłaby odpowiednia buzka.
Wiem tyle, że trzeba użyć takich znaków jak ?^* tylko nie wiem jak to zastosować do tego kodu (IMG:http://forum.php.pl/style_emoticons/default/sad.gif)
Go to the top of the page
+Quote Post
l0ud
post
Post #2





Grupa: Zarejestrowani
Postów: 1 387
Pomógł: 273
Dołączył: 18.02.2008

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


Użyj ereg_replace() z wyrażeniami regularnymi

  1. <?php
  2. $string = ':)))) :(((( :]]] :]';
  3.  
  4. $string = ereg_replace(':()+)','<img src="smile.gif"/>',$string);
  5. $string = ereg_replace(':((+)','<img src="eye.gif"/>',$string);
  6. $string = ereg_replace(':(]+)','<img src="sad.gif"/>',$string);
  7. ?>


Jak jeżeli ma zmieniać na podstawie tablic, musisz użyć pętli (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

Ten post edytował l0ud 12.03.2008, 22:19:51
Go to the top of the page
+Quote Post
webdice
post
Post #3


Developer


Grupa: Moderatorzy
Postów: 3 045
Pomógł: 290
Dołączył: 20.01.2007




Cytat(l0ud @ 12.03.2008, 21:56:33 ) *
Użyj ereg_replace() z wyrażeniami regularnymi

  1. <?php
  2. $string = ':)))) :(((( :]]] :]';
  3.  
  4. $string = ereg_replace(':()+)','<img src="smile.gif"/>',$string);
  5. $string = ereg_replace(':((+)','<img src="sad.gif"/>',$string);
  6. $string = ereg_replace(':(]+)','<img src="eye.gif"/>',$string);
  7. ?>


Jak jeżeli ma zmieniać na podstawie tablic, musisz użyć pętli (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)


~l0ud Twój kod nie zadziała. Zrób tak:

  1. <?php
  2. $string = preg_replace ('#:){1,}#', '<img src="smile.gif"/>', $string);
  3. $string = preg_replace ('#:]{1,}#', '<img src="sad.gif"/>',  $string);
  4. $string = preg_replace ('#:({1,}#', '<img src="eye.gif"/>',  $string);
  5. ?>
Go to the top of the page
+Quote Post
bartg
post
Post #4





Grupa: Zarejestrowani
Postów: 226
Pomógł: 25
Dołączył: 4.07.2007
Skąd: Berlin

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


Wydaje mi się że niektóre ( ) [ ] trzeba poprzedzić slashem (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

EDIT: Ale zapłon ;p

Ten post edytował bartg 12.03.2008, 22:11:06
Go to the top of the page
+Quote Post
l0ud
post
Post #5





Grupa: Zarejestrowani
Postów: 1 387
Pomógł: 273
Dołączył: 18.02.2008

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


Erm, webdicepl, nie wrzuciłbym kodu bez sprawdzenia, nie pisząc o tym...

[edit] Zwracam Ci jednak honor (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg) Przeglądarka albo forum z nieznanych mi przyczyn usunęło slashe, ale już poprawiłem (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

Ten post edytował l0ud 12.03.2008, 22:20:46
Go to the top of the page
+Quote Post
bartg
post
Post #6





Grupa: Zarejestrowani
Postów: 226
Pomógł: 25
Dołączył: 4.07.2007
Skąd: Berlin

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


l0ud:
Kod
Warning: ereg_replace() [function.ereg-replace]: REG_BADRPT in G:\!xampp\htdocs\konta\test.php on line 31


Kod działający w 100%:
  1. <?php
  2. $string = ':)))) :(((( :]]] :]';
  3.  
  4. $string = preg_replace('!:()+)!','<img src="smile.gif"/>',$string);
  5. $string = preg_replace('!:((+)!','<img src="sad.gif"/>',$string);
  6. $string = preg_replace('!:(]+)!','<img src="eye.gif"/>',$string);
  7. $string = preg_replace('!:(]+)!','<img src="eye.gif"/>',$string);
  8. ?>


EDIT: Sorry ale jak sprawdzałem kod to już klikłem nowy post i twojego edita nie widziałem (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

Ten post edytował bartg 12.03.2008, 22:24:26
Go to the top of the page
+Quote Post
l0ud
post
Post #7





Grupa: Zarejestrowani
Postów: 1 387
Pomógł: 273
Dołączył: 18.02.2008

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


bartg, swoją drogą Ci również slashe ucięło (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg) Może warto by było to gdzieś zgłosić, bo to chyba jakiś błąd forum jest?
Go to the top of the page
+Quote Post
webspy
post
Post #8





Grupa: Zarejestrowani
Postów: 186
Pomógł: 9
Dołączył: 3.03.2008

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


Dzięki, wszystko działa jak powinno (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) pozdrawiam.
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: 26.09.2025 - 03:22