php str_replace() 与关联数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2399286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:19:43  来源:igfitidea点击:

str_replace() with associative array

php

提问by Qiao

You can use arrays with str_replace():

您可以将数组与 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.

如何将它与 str_replace() 一起使用?
速度很重要 - 阵列足够大。

回答by Matthew

$text = strtr($text, $array_from_to)

$text = strtr($text, $array_from_to)

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

顺便说一下,这仍然是一个一维“数组”。

回答by mauris

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

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

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

to字段将忽略数组中的键。这里的关键功能是array_keys

回答by Rahul Yadav

$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

回答by Mark Tomlin

$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..

结果在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.

扩展第一个示例,我们将 $search 作为数组的键,将 $replace 作为值,代码如下所示。

$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..

结果在Hello Qiao, welcome to stackoverflow..

回答by Tyler Carter

$keys = array_keys($array);
$values = array_values($array);
$text = str_replace($key, $values, $string);