2013-07-31 58 views
0

为什么这段代码无法正常工作?我试图重命名,切换位置和其他,但它似乎是str_replace错误。这将是很好,如果有人告诉我,什么是错的... 这是我的index.php我的代码与str_replace不起作用

<?php 
header('Content-Type:text/html;charset=utf-8'); 
session_start(); 

require_once ('inc/core.php'); 
$core = new core($_GET['viev']); 

这是core.php中

<?php 
class core{ 

    var $template; 
    var $view; 

    public function __construct($view) { 
     $this->template = $this->loadTemplate(); 
     $this->view = $view; 
     $this->loadView(); 
     echo $this->template; 
    } 

    private function loadTemplate(){ 
     if(file_exists('template/template.html')){ 
      $template = file_get_contents('template/template.html'); 
     } 
     else{ 
      $template = 'Coś poszło nie tak z szablonem ;/'; 
     } 
     return $template; 
    } 

    private function loadView(){ 
     global $core;    
     $core = $this; 

     if($this->view == ""){ 
      $this->view = "home"; 
     } 
     if(file_exists('inc/view/'.$this->view.'.php')){ 
      require_once ('inc/view/'.$this->view.'.php'); 
     } 
     else{ 
      str_replace('{{page.content}}', 'Wybacz, wygląda na to, że podałeś zły adres ;(', $this->template); 
     } 
    } 

    public function ViewReplace($replace){ 
     if(strpos($this->template, '{{page.content}}') !== FALSE){ 
      str_replace('{{page.content}}', $replace, $this->template); 
     } 
    } 
} 

这是home.php

例如
<?php 
$core->ViewReplace(homeView()); 

function homeView(){ 
    global $core; 
    return '<article> 
    <h2>Witaj na stronie serwera Snowcraft!</h2> 
    <a href="https://www.facebook.com/snowcraftpl" class="button">Odwiedz nasz "fanpejdz" na facebook-u</a> 
    <p>Serwer Snowcraft.pl to nowy pomysł na serwer Minecraft. Wywodzi się z połączenie kilku pomysłów i zrealizowania ich w gronie wieloosobowej administracji.</p> 
    <h4>Cos wiecej</h4> 
    <p>Nasz serwer jest fuzją serwerów typu "minez" i "paintball". Grać na nim może jednocześnie wiele graczy, a cała rozgrywka została zrobiona tak, by sprawiać wam jak największą przyjemność.<br> 
    Oto, byście mogli na nim grać bez przeszkód dba grupa w której skład wchodzą:<br> 
    Załorzyciel-Kiwiszon;<br> 
    HeadAdmin-TheKrzywda;<br> 
    Admini-;<br> 
    Moderatorzy-;</p> 
</article>'; 
} 

我没有在现场的任何错误,但这个{{page.content}}不工作,我不知道为什么;(

而且也对不起坏英语;/

+0

欢迎SO!如果你能想出一个简单的例子来说明你的问题,而不是发布你所有的代码,这将是非常有帮助的。你的问题会更容易阅读,你会得到更好的答案。如果问题出现在'str_replace'中,请尝试用两三行代码重现它。 – eaj

回答

4

一方面str_replace()回报替换字符串,它不会修改原始字符串,以便写在创建新的替代值,然后就扔掉了,因为你尚未将返回值分配给任何内容。您需要将模板值设置为替换值:

$this->template = str_replace('{{page.content}}', $replace, $this->template); 
+0

非常感谢Ben D. $ this-> template = ...正在工作。 FUnny,因为这是一个小错误,所有的代码都是错的。今天你是我的英雄;) –

+1

很高兴我可以帮助:) –

-1

听到的是str_replace函数的一些例子,我希望这将帮助你很多

<?php 
// Provides: <body text='black'> 
$bodytag = str_replace("%body%", "black", "<body text='%body%'>"); 

// Provides: Hll Wrld f PHP 
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"); 
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP"); 

// Provides: You should eat pizza, beer, and ice cream every day 
$phrase = "You should eat fruits, vegetables, and fiber every day."; 
$healthy = array("fruits", "vegetables", "fiber"); 
$yummy = array("pizza", "beer", "ice cream"); 

$newphrase = str_replace($healthy, $yummy, $phrase); 

// Provides: 2 
$str = str_replace("ll", "", "good golly miss molly!", $count); 
echo $count; 
?> 
+1

这是PHP文档的精确副本。 –