Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Jak zamienić pewien string na nicość.., czyli znów i love preg_replace
[PL]Neon
post
Post #1





Grupa: Zarejestrowani
Postów: 23
Pomógł: 0
Dołączył: 20.03.2005
Skąd: Nowy Dwór Maz.

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


Znów kłopot.. Jak zamienić taki kod :

Kod
<!-- POCZATEK # NIEZALOGOWANY --> *kod html* <!-- KONIEC # NIEZALOGOWANY -->


na NICOŚĆ przy pomocy preg_replace ?

Otóż chcę zamienić wszystko co pomiędzy włącznie z <!-- POCZATEK .. --> i <!-- KONIEC .. --> na NICOŚĆ

Ten post edytował [PL]Neon 5.11.2005, 20:29:26


--------------------
"Wszystko powinno zostać uproszczone tak bardzo, jak to tylko możliwe, ale nie bardziej"
Albert Einstein
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 9)
dtb
post
Post #2





Grupa: Zarejestrowani
Postów: 476
Pomógł: 1
Dołączył: 5.11.2005
Skąd: Bieruń city

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


zamień:
Kod
<!-- POCZATEK # NIEZALOGOWANY -->([\n\ta-zA-Z0-9ąśćńźżęóĄŚĆŃŹŻÓ\!\@\#\$\%\*\^&\*\(\)\\\[\]\;\,\.\/\?\:\{\}\_\-\=\+\| ]+){0,}<!-- KONIEC # NIEZALOGOWANY -->


na:
Kod
<!-- POCZATEK # NIEZALOGOWANY --><!-- KONIEC # NIEZALOGOWANY -->


  1. <?php
  2. $text = preg_replace(
  3. '<!-- POCZATEK # NIEZALOGOWANY -->([nta-zA-Z0-9ąśćńźżęóĄŚĆŃŹŻÓ!@#$%*^&*()[];,./?:{}_-=+| ]+){0,}<!-- KONIEC # NIEZALOGOWANY -->',
  4. '<!-- POCZATEK # NIEZALOGOWANY --><!-- KONIEC # NIEZALOGOWANY -->',
  5. $text);
  6. ?>


Powinno działać

Ten post edytował dtb 5.11.2005, 22:15:53


--------------------
Go to the top of the page
+Quote Post
bela
post
Post #3


Administrator PHPedia.pl


Grupa: Developerzy
Postów: 1 102
Pomógł: 2
Dołączył: 14.09.2003

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


Kod
<!-- POCZATEK # NIEZALOGOWANY -->([\n\ta-zA-Z0-9ąśćńźżęóĄŚĆŃŹŻÓ\!\@\#\$\%\*\^&\*\(\)\\\[\]\;\,\.\/\?\:\{\}\_\-\=\+\| ]+){0,}<!-- KONIEC # NIEZALOGOWANY -->


A nie można?
Kod
<!-- POCZATEK # NIEZALOGOWANY -->(.*)<!-- KONIEC # NIEZALOGOWANY -->


--------------------
Go to the top of the page
+Quote Post
FiDO
post
Post #4





Grupa: Przyjaciele php.pl
Postów: 1 717
Pomógł: 0
Dołączył: 12.06.2002
Skąd: Wolsztyn..... Studia: Zielona Góra

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


dtb: "troche" glupio napisany te Twoje wyrazenie.. no i przesadziles troche z escape'owaniem wszystkich znakow.

Mozna sporo krocej:
  1. <?php
  2.  
  3. $text = preg_replace('%(<!-- POCZATEK # NIEZALOGOWANY -->).*(<!-- KONIEC # NIEZALOGOWANY -->)%U', '12', $text);
  4.  
  5. ?>


--------------------
Brak czasu :/
Go to the top of the page
+Quote Post
[PL]Neon
post
Post #5





Grupa: Zarejestrowani
Postów: 23
Pomógł: 0
Dołączył: 20.03.2005
Skąd: Nowy Dwór Maz.

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


Niestety żaden z waszych przykładów nie działa..

Jak tabela z logowaniem dla niezalogowanych była, tak dalej jest..

Ten post edytował [PL]Neon 6.11.2005, 10:20:26


--------------------
"Wszystko powinno zostać uproszczone tak bardzo, jak to tylko możliwe, ale nie bardziej"
Albert Einstein
Go to the top of the page
+Quote Post
dtb
post
Post #6





Grupa: Zarejestrowani
Postów: 476
Pomógł: 1
Dołączył: 5.11.2005
Skąd: Bieruń city

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


A nie lepiej użyc jakiegoś systemu szablonów, i użyć instukcji {if} ?


--------------------
Go to the top of the page
+Quote Post
ghostrider
post
Post #7





Grupa: Zarejestrowani
Postów: 135
Pomógł: 0
Dołączył: 30.09.2005
Skąd: k-rk

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


modyfikatory
może i : ignore case i m : multiline

czyli
  1. <?php
  2.  
  3. $regex = "/(<!-- POCZATEK # NIEZALOGOWANY -->)(.*?)(<!-- KONIEC # NIEZALOGOWANY -->)/im";
  4. ?>


spróbuj


--------------------
There are 10 types of people in the world:
-Those who understand binary, and those who don't...
There's no place like 127.0.0.1
Go to the top of the page
+Quote Post
[PL]Neon
post
Post #8





Grupa: Zarejestrowani
Postów: 23
Pomógł: 0
Dołączył: 20.03.2005
Skąd: Nowy Dwór Maz.

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


Dalej nie działa :/

Może to moja wina, więc daję kod :

plik .tpl :
Kod
    
    <!-- POCZATEK # NIEZALOGOWANY -->
    {HELLO}<b>{GUEST}</b>
    <div style='margin-top: 8px;'>
    <form method='post' action='index.php' name='login' onsubmit='return check_login_form();'>
    <table cellpadding='0' cellspacing='0' style='width: 199px; height: 50px; border: 0px;'>
      <tr>
        <td style='vertical-align: top; width: 50px; height: 50px; border: 0px;'>
         <img src='avatars/no_avatar.gif' width='50' alt='' border='' /><br />
        </td>
        <td style='width: 84px; height: 50; border: 0px; text-align: left;'>
        <table cellpadding='0' cellspacing='0' style='width: 84px; height: 50px; border: 0px; margin-right: 11px;'>
          <tr>
            <td style='vertical-align: top;'>
             <div style='margin-left: 12px; margin-top: 2px;'>
             <input name='loginNick' size='16' class='inputLogin' type='text' value='{LOGIN_VALUE_1}' />
             </div>
            </td>
          </tr>
          <tr>
            <td style='vertical-align: bottom;'>
             <div style='margin-left: 12px; margin-bottom: 2px;'>
             <input name='loginPass' size='16' class='inputLogin' type='password' value='{LOGIN_VALUE_2}' />
             </div>
            </td>
          </tr>
        </table>
        </td>
        <td style='vertical-align: middle; width: 84px; height: 25%; border: 0px;'>
         <div align='center'>
         <input name='loginSubmit' type='image' value='submit' src='template/gfx/input_login.gif' style='width: 52px; height: 22px; margin: 0px; padding: 0px;' />
         </div>
        </td>
      </tr>
    </table>
    </form>
    </div>
    <div class='userOptions' style='text-align: right; margin-top: 9px; margin-right: 8px;'>
    <a href='user.php?mode=register' title='{TOOLTIP_REGISTER}'>
    {REGISTER}
    </a><br />
    <a href='user.php?mode=lostpassword' title='{TOOLTIP_LOST_PASS}'>
    {LOST_PASS}
    </a>
    </div>
    </div>
    </td>
    <!-- KONIEC # NIEZALOGOWANY -->
    <!-- POCZATEK # ZALOGOWANY -->
    <!-- KONIEC # ZALOGOWANY -->


obsługa .tpl (funkcja w classie) :
  1. <?php
  2. function showcode ($handle)
  3. {
  4. global $config;
  5. global $aaa;
  6.  
  7. if ( !isset($this->code[$handle]) ) // No assign_vars ?
  8. {
  9. if (!$this->loadfile($handle))
  10. {
  11. die ('Wystąpił błąd przy załadowywaniu .tpl (' .$handle. ')');
  12. }
  13.  
  14. if ( $config['show_tooltip'] == 0 )
  15. {
  16. $this->code[$handle] = preg_replace('/ title=("|').*1/Uis', '', $this->code[$handle]);
  17. }
  18.  
  19. if ( $aaa == 'asdasd' )
  20. {
  21. $this->code[$handle] = preg_replace('%(<!-- POCZATEK # NIEZALOGOWANY -->).*(<!-- KONIEC # NIEZALOGOWANY -->)%U', '12', $this->code[$handle]);
  22. }
  23. else
  24. {
  25. $this->code[$handle] = preg_replace('%(<!-- POCZATEK # ZALOGOWANY -->).*(<!-- KONIEC # ZALOGOWANY -->)%U', '12', $this->code[$handle]);
  26. }
  27.  
  28. echo $this->code[$handle];
  29. }
  30. else // There was assign_vars
  31. {
  32. if ( $config['show_tooltip'] == 0 )
  33. {
  34. $this->code[$handle] = preg_replace('/ title=("|').*1/Uis', '', $this->code[$handle]);
  35. }
  36.  
  37. if ( $aaa == 'asdasd' )
  38. {
  39. $this->code[$handle] = preg_replace("/(<!-- POCZATEK # NIEZALOGOWANY -->)(.*?)(<!-- KONIEC # NIEZALOGOWANY -->)/im", '', $this->code[$handle]);
  40. }
  41. else
  42. {
  43. $this->code[$handle] = preg_replace('/(<!-- POCZATEK # NIEZALOGOWANY -->)(.*?)(<!-- KONIEC # NIEZALOGOWANY -->)/im', '', $this->code[$handle]);
  44. }
  45.  
  46. echo $this->code[$handle];
  47. }
  48. ?>


I niestety nic się nie dzieje.. Ani .. NIEZALOGOWANY .. ani .. ZALOGOWANY .. nie jest tykane..

Ten post edytował [PL]Neon 6.11.2005, 15:04:40


--------------------
"Wszystko powinno zostać uproszczone tak bardzo, jak to tylko możliwe, ale nie bardziej"
Albert Einstein
Go to the top of the page
+Quote Post
ghostrider
post
Post #9





Grupa: Zarejestrowani
Postów: 135
Pomógł: 0
Dołączył: 30.09.2005
Skąd: k-rk

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


dodałem modifikator s - PCRE_DOTALL, także . (kropek) pasuje takze do znaków nowej lini
  1. <?php
  2. $string = "<!-- POCZATEK # NIEZALOGOWANY -->
  3. to brednie sa jakieś ...
  4. jestme przecierz zalog !!!
  5. <!-- KONIEC # NIEZALOGOWANY -->
  6. ";
  7.  
  8. print preg_replace("/<!-- POCZATEK # NIEZALOGOWANY -->.*<!-- KONIEC # NIEZALOGOWANY -->/ims", '', $string);
  9. ?>


i działa, przynajmniej na tym przykładzie, powinno i w twoim kodzie, napisz jak jest smile.gif

Ten post edytował ghostrider 6.11.2005, 15:26:06


--------------------
There are 10 types of people in the world:
-Those who understand binary, and those who don't...
There's no place like 127.0.0.1
Go to the top of the page
+Quote Post
bendi
post
Post #10





Grupa: Zarejestrowani
Postów: 401
Pomógł: 5
Dołączył: 14.09.2003
Skąd: Wrocław

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


Cytat(ghostrider @ 2005-11-06 15:23:58)
modyfikatory
może i : ignore case i m : multiline

Chyba nie zrozumiałeś jak działa multiline - wymusza traktowanie danego ciągu jak ciągu wielliniowego czyli jest przeciwieństwem tego czego oczekujemy od tej regułki, więc pozbądź się z ostatniej regułki m i dodaj samo s, które to wymusza traktowanie ciągu jako jednoliniowego, czyli znaki nowej linii nie kończą przetwarzania wyrażenia.


--------------------
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 - 14:13