我的代码与str_replace don';不起作用


My code with str_replace don't work

为什么此代码不起作用?我试图重命名,切换位置和其他,但似乎是str_replace错误。如果有人告诉我怎么了,那就太好了。。。这是我的index.php

<?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}}不起作用,我不知道为什么;(

同时,为糟糕的英语感到抱歉;/

首先,str_replace()返回被替换的字符串,它不会修改原始字符串,因此在写入时会创建新的被替换值,然后因为您没有将返回值分配给任何东西而被丢弃。您需要将模板值设置为替换的值:

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

hear是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;
?>