codeigniter - PHP - concatenate string from database -
i have string send email registration. string stored in database.
when load string model.
//string db hi {$email} , bla bla bla
my controllers example.
function (){ $text = $this->some_model->get_string(); // var post $email = 'foo@mail.com' echo $text; }
and there no change whatsoever in string. str_replace cant output this.
hi foo@mail.com, bla bla bla
what if case. have 2 or more string replace?
example
hi {$email}, {$username}
php doesnt magically check each string if contains code can execute. if string in database contains hi {$email} , bla bla bla
php leave that.
so need manually override values in string match values. safest way simple str_replace
<?php $string = 'hi {$email} , bla bla bla'; $email = 'foo@example.com'; $output = str_replace('{$email}', $email, $string); $output = str_replace('{$username}', $username, $output ); echo $output; //hi foo@example.com , bla bla bla ?>
or in code
<?php function (){ $text = $this->some_model->get_string(); $email = 'foo@mail.com'; echo str_replace('{$username}', $username, str_replace('{$email}', $email, $text)); //hi foo@mail.com , bla bla bla } ?>
if create string (with double quotes) before put in database, value of {$email}
have been changed
<?php $email = 'foo@mail.com'; $text = "hi {$email} , bla bla bla"; //notice double quotes, single qoutes wont work echo $text; //hi foo@mail.com , bla bla bla ?>
Comments
Post a Comment