Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Problem z str_replace
Forum PHP.pl > Forum > PHP
Hpsi
Witam , mam problem robię filtrację pewnego kawalku kodu no i zamiast wyświetlić mi kod przefiltrowany pokazuje mi Array. Dlaczego ?

Plik robiący filtracje
  1. <?php
  2.  
  3.     class show
  4.         {
  5.      function show()
  6.      {
  7.      $this->emot = &$GLOBALS['table']['emot'];
  8.     }
  9.        function emot()
  10.                 {
  11.                  $res = mysql_query(&#092;"SELECT * FROM { $this->emot}\");
  12.                  echo mysql_error();
  13.                  $re = mysql_fetch_array($res);
  14.                  if($re['emot'] == '-') return false;
  15.                   if($re['emot'] == '+') return true;
  16.                 }
  17.             
  18.                    function k_news($ret, $r, $kom, $text) 
  19.                     {
  20.                    #if(show::emot() == true) show::str_emot($r);
  21.                    $ret = str_replace('{news:title}', $r['title'], $ret);
  22.                    $ret = str_replace('{news:data}', $r['data'], $ret);
  23.                    $ret = str_replace('{news:ktext}', $r['ktext'], $ret);
  24.                    $ret = str_replace('{news:pirint}', '<a href=\"print.php?a=news&amp;id='.$r['id'].'\"><img src=\"theme/news/gxf/print.gif\" alt=\"\" border=\"0\" /></a>', $ret);
  25.                   $ret = str_replace('{news:autor}', $r['autor'], $ret);
  26.                    $ret = str_replace('{news:kom}', '<a href=\"news.php?a=kom&amp;id='.$r['id'].'\">'.$text['kom'].' ('.$kom.')</a>', $ret);
  27.                    if($r['dtext'] != '')
  28.                    {
  29.                   $ret = str_replace('{news:stext}', '<a href=\"news.php?a=show&amp;id='.$r['id'].'\">'.$text['more'].'', $ret);
  30.                                         
  31.                   } // if
  32.                   return $ret;
  33.                     } // function
  34. } // class
  35.  
  36.  
  37. ?>

Plik który ma być przefiltorwany
  1. <div class="tyt"><p>{news:titile}{news:data}</p></div>
  2. <div class="news"><p>{news:ktext}</p></div>
  3. <div class="newsd">{news:more}{news:print}{news:kom}<p></p></div>


Natomiast Plik wyświetlający:
  1. <?php
  2.  
  3. class news
  4. {
  5.     function news()
  6.     {
  7.         $this->news = &$GLOBALS['table']['news'];
  8.         $this->limit = &$GLOBALS['table']['limit'];
  9.         $this->kom = &$GLOBALS['table']['kom'];
  10.     } // function
  11.     function limit()
  12.     {
  13.         $res = mysql_query(&#092;"SELECT * FROM {$this->limit} WHERE co='news'\");
  14.         $rr = mysql_fetch_array($res);
  15.         $limit = $rr['ile'];
  16.  
  17.         return $limit;
  18.     } // function
  19.  
  20.     function show()
  21.     {
  22.         $limit = news::limit();
  23.         include_once('news_lib/news_str.lib.php');
  24.  
  25.         $resul = mysql_query(&#092;"SELECT * FROM {$this->news} WHERE act='1'  ORDER BY id DESC\");
  26.         echo mysql_error();
  27.         $numer = mysql_num_rows($resul);
  28.         $pages = ceil($numer/$limit);
  29.         $page = intval($_GET['page'] );
  30.         if ($page < 1 || $page > $pages)
  31.         {
  32.             $page = 1;
  33.         } // if
  34.         $ret = file('theme/news/news.th.php');
  35.         $text = file('news_lib/teksty.lib.php');
  36.         $result = mysql_query(&#092;"SELECT * FROM {$this->news} WHERE act='1' ORDER BY id DESC LIMIT {$limit} OFFSET \".(($page-1)*$limit));
  37.         echo mysql_error();
  38.         while($r = mysql_fetch_array($result))
  39.         {
  40.             $komm = mysql_query(&#092;"SELECT * FROM {$this->kom} WHERE news='{$r['id']}'\");
  41.             $kom = mysql_num_rows($komm);
  42.             echo show::k_news($ret, $r, $kom, $text);
  43.  
  44.         }
  45.     } // function
  46. } // class
  47.  
  48. ?>
nospor
linijka 35 ostatniego kodu: $ret = file('theme/news/news.th.php');
w wyniku tego otrzymujesz tablicę. Musisz to jeszcze zamienic na string (gdyż str_replace operuje na stringu), czyli
  1. <?php
  2.  
  3. $ret = file('theme/news/news.th.php');
  4. $ret=implode('',$ret);
  5.  
  6. ?>


Edit: albo nie muszisz tego zamieniać na string, tylko tą tablicę co otrzymasz po wykonaniu show::k_news zamien na string (metoda implode - patrz wyzej)
Hpsi
Dzięki !:)) naprawde wielkie dzięki. A ja się meczłem z tym 3 h ... ;P
brajan
Można też krócej:

Kod
$ret = implode(file('theme/news/news.th.php'));


tongue.gif
Fipaj
jest jeszcze file_get_contents() - moim zdaniem najlepsze rozwiązanie
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.