質問

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']);

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);
        }
    }
}

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>';
}

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 ;/

役に立ちましたか?

解決

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);

他のヒント

hear is some example of str_replace i hope this will help you a Lot

<?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;
?>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top