我的str_replace代码不起作用 [英] My code with str_replace don't work

查看:104
本文介绍了我的str_replace代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

why this code isn't working? I was trying to rename, switch location and other, but it seems to be str_replace bug. It would be nice, if somebody told me, what's wrong... This is my 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

this is 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的示例

And this is example for 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>';
}

我的网站上没有任何错误,但是这个{{页面。 content}}不起作用,我也不知道为什么;(

I don't have any bugs on site, but this {{page.content}} isn't working and i don't know why ;(

而且,对不起,英语不好; /

And also, sorry for bad English ;/

推荐答案

对于 str_replace() 返回的一件事,它没有修改原始字符串,以便在编写时创建新的替换值,然后将其丢弃,因为您尚未将返回值分配给任何东西。您需要将模板值设置为替换值:

For one thing str_replace() returns the replaced string, it doesn't modify the original string, so as written the new replaced value is created and then just thrown away because you haven't assigned the return value to anything. You need to set the template value to the replaced value:

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

这篇关于我的str_replace代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆