Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing {{string}} within php file

I'm including a file in one of my class methods, and in that file has html + php code. I return a string in that code. I explicitly wrote {{newsletter}} and then in my method I did the following:

$contactStr = include 'templates/contact.php';
$contactStr = str_replace("{{newsletter}}",$newsletterStr,$contactStr);

However, it's not replacing the string. The only reason I'm doing this is because when I try to pass the variable to the included file it doesn't seem to recognize it.

$newsletterStr = 'some value';
$contactStr = include 'templates/contact.php';

So, how do I implement the string replacement method?

like image 620
somejkuser Avatar asked Jul 25 '13 22:07

somejkuser


People also ask

How can I replace part of a string in PHP?

The str_replace() function replaces some characters with some other characters in a string. This function works by the following rules: If the string to be searched is an array, it returns an array. If the string to be searched is an array, find and replace is performed with every array element.

How can I replace multiple characters in a string in PHP?

Approach 1: Using the str_replace() and str_split() functions in PHP. The str_replace() function is used to replace multiple characters in a string and it takes in three parameters. The first parameter is the array of characters to replace.

How do I remove a word from a string in PHP?

Answer: Use the PHP str_replace() function You can use the PHP str_replace() function to replace all the occurrences of a word within a string.

Which function is used to replace pattern in string in PHP?

The preg_replace() function returns a string or array of strings where all matches of a pattern or list of patterns found in the input are replaced with substrings. There are three different ways to use this function: 1. One pattern and a replacement string.


1 Answers

You can use PHP as template engine. No need for {{newsletter}} constructs.

Say you output a variable $newsletter in your template file.

// templates/contact.php

<?php echo $newsletter; ?>

To replace the variables do the following:

$newsletter = 'Your content to replace';

ob_start();        
include('templates/contact.php');
$contactStr = ob_get_clean();

echo $contactStr;

// $newsletter should be replaces by `Your content to replace`

In this way you can build your own template engine.

class Template
{
    protected $_file;
    protected $_data = array();

    public function __construct($file = null)
    {
        $this->_file = $file;
    }

    public function set($key, $value)
    {
        $this->_data[$key] = $value;
        return $this;
    }

    public function render()
    {
        extract($this->_data);
        ob_start();
        include($this->_file);
        return ob_get_clean();
    }
}

// use it
$template = new Template('templates/contact.php');
$template->set('newsletter', 'Your content to replace');
echo $template->render();

The best thing about it: You can use conditional statements and loops (full PHP) in your template right away.

Use this for better readability: https://www.php.net/manual/en/control-structures.alternative-syntax.php

like image 86
bitWorking Avatar answered Sep 18 '22 13:09

bitWorking