Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Usuwanie powtarzających się znaków...
Rafael6666
post
Post #1





Grupa: Zarejestrowani
Postów: 95
Pomógł: 0
Dołączył: 20.10.2006

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


Mam np. ciąg znaków: "aala ma kkkkota", jak za pomocą preg_replace zmienić ten tekst na "ala ma kota".


--------------------
Kompleksowe i profesjonalne tworzenie stron www
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
siemakuba
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 1 112
Pomógł: 20
Dołączył: 10.04.2005

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


  1. <?php
  2. $txt = 'aaala ma kkkotaaa';
  3. $txt = preg_replace('/(.+)1{1,}/i', "$1", $txt);
  4. ?>


pozdr.
Go to the top of the page
+Quote Post
Raisen
post
Post #3





Grupa: Zarejestrowani
Postów: 1
Pomógł: 0
Dołączył: 19.08.2009

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


Wiem, że temat jest stary, ale miałem podobny problem i nigdzie nie mogłem znaleźć rozwiązania.

Może komuś moje rozwiązanie się przyda.

  1. function strip_multiplied_characters ($str,$character=null) {
  2. /*
  3. @author: Raisen
  4. @e-mail: kontakt[at]raisen[dot-here]com[dot-here]pl
  5. */
  6. if (is_null($character) && is_string($str) && strlen($str)>0) {
  7. return preg_replace('/(?<znak>.)\k<znak>+/',"$1",$str);
  8. }
  9.  
  10. if (is_string($character) && strlen($character)==1 && is_string($str) && strlen($str)>0) {
  11. return preg_replace('/(?<znak>'.$character.')\k<znak>+/',"$1",$str);
  12. }
  13.  
  14. return null;
  15. }
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 Aktualny czas: 20.08.2025 - 19:15