My code with str_replace don't work

前提是你 提交于 2020-01-11 11:56:07

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/17980104/my-code-with-str-replace-dont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!