Reputation: 21
I need to replace certain characters.
$to_replace = array( "{username}", "{email}" );
$replace_with = array( $username, $email );
Also, $key_value
is an array which gives me array key and values like:
array(
'site' => 'abc.com',
'blog' => 'blog.com'
'roll' => 42
);
Using
$message = 'This is a {username} speaking, my email is {email}, and my site is {site} with roll {roll}';
$message = str_replace( $to_replace, $replace_with, $message );
This way I can replace username and email, How can I make it to site, blog and roll?
Thanks!
Upvotes: 1
Views: 84
Reputation: 43574
You can use the following solution:
$email = '[email protected]';
$username = 'johndoe';
$to_replace = array( "{username}", "{email}" );
$replace_with = array( $username, $email );
$key_value = array(
'site' => 'abc.com',
'blog' => 'blog.com',
'roll' => 42
);
//add the keys and values from $key_value to the replacement arrays.
$to_replace = array_merge($to_replace, array_keys($key_value));
$replace_with = array_merge($replace_with, array_values($key_value));
//surround every key with { and }.
array_walk($to_replace, function(&$value, $key) { $value = '{'.trim($value, '{}').'}';});
$message = 'This is a {username} speaking, my email is {email}, and my site is {site} with roll {roll}';
$message = str_replace( $to_replace, $replace_with, $message );
var_dump($message); //This is a johndoe speaking, my email is [email protected], and my site is abc.com with roll 42
Upvotes: 1
Reputation: 4205
You can use extract function to achieve this in easiest possible way.
And, try to wrap the message logic in a separate function for sanitising data and avoiding variable name conflicts.
function createMessage($username, $email, $userMeta) {
extract($userMeta);
return "This is a {$username} speaking, my email is {$email}, and my site is {$site} with roll {$roll}";
}
$username = 'awesome';
$email = '[email protected]';
$userMeta = array(
'site' => 'abc.com',
'blog' => 'blog.com',
'roll' => 42
);
echo createMessage($username, $email, $userMeta);
// This is a awesome speaking, my email is [email protected], and my site is abc.com with roll 42
Warning: Do not use extract() on untrusted data, like user input (e.g. $_GET, $_FILES).
Upvotes: 1
Reputation: 57121
It would be so much easier to just populate the first two arrays with all of the data you want to replace, but if your stuck with what you have so far, then this will do the job.
This takes the keys of the last array (using array_keys()
) and adds {} round it using array_walk()
. And the array values and merges both these into the original arrays.
$to_replace = array( "{username}", "{email}" );
$replace_with = array( $username, $email );
$extra = array(
'site' => 'abc.com',
'blog' => 'blog.com',
'roll' => 42
);
$extraTo = array_keys($extra);
array_walk($extraTo, function(&$d) { $d= "{".$d."}";});
$to_replace = array_merge($to_replace, $extraTo);
$replace_with = array_merge($replace_with, array_values($extra));
$message = 'This is a {username} speaking, my email is {email}, and my site is {site} with roll {roll}';
$message = str_replace( $to_replace, $replace_with, $message );
echo $message;
Upvotes: 1