Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Jak zamienić pewien string na nico¶ć..
Forum PHP.pl > Forum > PHP
[PL]Neon
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¦Ć
dtb
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ć
bela
Kod
<!-- POCZATEK # NIEZALOGOWANY -->([\n\ta-zA-Z0-9±¶ćńĽżęóˇ¦ĆѬŻÓ\!\@\#\$\%\*\^&\*\(\)\\\[\]\;\,\.\/\?\:\{\}\_\-\=\+\| ]+){0,}<!-- KONIEC # NIEZALOGOWANY -->


A nie można?
Kod
<!-- POCZATEK # NIEZALOGOWANY -->(.*)<!-- KONIEC # NIEZALOGOWANY -->
FiDO
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. ?>
[PL]Neon
Niestety żaden z waszych przykładów nie działa..

Jak tabela z logowaniem dla niezalogowanych była, tak dalej jest..
dtb
A nie lepiej użyc jakiego¶ systemu szablonów, i użyć instukcji {if} ?
ghostrider
modyfikatory
może i : ignore case i m : multiline

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


spróbuj
[PL]Neon
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..
ghostrider
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
bendi
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.
To jest wersja lo-fi głównej zawarto¶ci. Aby zobaczyć pełn± wersję z większ± zawarto¶ci±, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.