38

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.

0

5 Answers 5

63

$text = strtr($text, $array_from_to)

By the way, that is still a one dimensional "array."

Sign up to request clarification or add additional context in comments.

3 Comments

it is not perfect solution for stated problem (cause lengths should be the same), but it is ideal in my case. And speed is fast.
strtr works fine with replacement values that differ in length from the search value. The difference between it and str_replace is that strtr will only do one translation (the longest is matched first), which will be faster (but with different results). E.g., ['ab' => 'c', 'c' => 'd'] will translate 'ab' to 'c', while with str_replace it will become 'd'.
another thing is strtr is quite fast because the PHP directly uses the C version which is believed to be implemented using look up tables
40
$array_from_to = array (
    'from1' => 'to1',
    'from2' => 'to2'
);

$text = str_replace(array_keys($array_from_to), $array_from_to, $text);

The to field will ignore the keys in your array. The key function here is array_keys.

1 Comment

Could $array_from_to be traversed in different order than what array_keys() return?
9
$text='yadav+RAHUL(from2';

  $array_from_to = array('+' => 'Z1',
                         '-' => 'Z2',
                         '&' => 'Z3',
                         '&&' => 'Z4',
                         '||' => 'Z5',
                         '!' => 'Z6',
                         '(' => 'Z7',
                         ')' => 'Z8',
                         '[' => 'Z9',
                         ']' => 'Zx1',
                         '^' => 'Zx2',
                         '"' => 'Zx3',
                         '*' => 'Zx4',
                         '~' => 'Zx5',
                         '?' => 'Zx6',
                         ':' => 'Zx7',
                         "'" => 'Zx8');

  $text = strtr($text,$array_from_to);

   echo $text;

 //output is

yadavZ1RAHULZ7from2

Comments

4
$search = array('{user}', '{site}');
$replace = array('Qiao', 'stackoverflow');
$subject = 'Hello {user}, welcome to {site}.';

echo str_replace ($search, $replace, $subject);

Results in Hello Qiao, welcome to stackoverflow..

$array_from_to = array (
    'from1' => 'to1',
    'from2' => 'to2'
);

This is not a two-dimensional array, it's an associative array.

Expanding on the first example, where we place the $search as the keys of the array, and the $replace as it's values, the code would look like this.

$searchAndReplace = array(
    '{user}' => 'Qiao',
    '{site}' => 'stackoverflow'
);

$search = array_keys($searchAndReplace);
$replace = array_value($searchAndReplace);
# Our subject is the same as our first example.

echo str_replace ($search, $replace, $subject);

Results in Hello Qiao, welcome to stackoverflow..

1 Comment

Parse error: syntax error, unexpected ';', expecting ')' in $array_from_to... :)
2
$keys = array_keys($array);
$values = array_values($array);
$text = str_replace($key, $values, $string);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.