Search code examples
phpstringcodeignitercodeigniter-2fwrite

Saving multiline strings that contain variables, into a file with PHP and CodeIgniter


I'm trying to save a multiline string, something like this in plain text:

define host {
        use                     template;
        host_name               $host_name;
        address                 46.224.2.220;
        contacts                $email;
}

but variables e.g. $host_name and $email must be replaced with their values. s

I couldn't figure this out. any way to achieve this?


Solution

  • You can use str_replace

    $plain_text = "define host {
            use                     template;
            host_name               $host_name;
            address                 46.224.2.220;
            contacts                $email;
    }";
    
    
    $find = array("$host_name", "$email");
    $replace = array($host_name, $email);
    
    $new_plain_text = str_replace($find, $replace, $plain_text);