Thursday 30 August 2018

PHP - str_replace () with associative array

You can use arrays with str_replace():

$array_from = array ('from1', 'from2');
$array_to = array ('to1', 'to2');

$text = str_replace ($array_from, $array_to, $text);

But what if you have associative array?
$array_from_to = array (
 'from1' => 'to1';
 'from2' => 'to2';
);

How can you use it with str_replace()?
Speed matters - array is big enough.

$text = strtr($text, $array_from_to)
By the way, that is still a one dimensional "array."

0 comments:

Post a Comment