$string = 'The quick brown fox jumped over the lazy dog.';
$patterns = array();
$patterns[2] = '/quick/';
$patterns[1] = '/brown/';
$patterns[0] = '/fox/';
$patterns[4] = '/lazy/';
var_dump($patterns);
$replacements = array();
$replacements[0] = 'lazyo';
$replacements[2] = 'black';
$replacements[1] = 'bear';
$replacements[4] = 'slow';

echo preg_replace($patterns, $replacements, $string);

会输出

array (size=4)
  2 => 

string

 '/quick/' (length=7)
  1 => 

string

 '/brown/' (length=7)
  0 => 

string

 '/fox/' (length=5)
  4 => 

string

 '/lazy/' (length=6)

The slowo black bear jumped over the slow dog.

所以preg_replace是根据数组的键来遍历替换的。

相关文章:

  • 2021-11-30
  • 2021-12-15
  • 2021-08-03
  • 2021-09-16
  • 2021-10-04
  • 2021-06-20
  • 2022-02-22
猜你喜欢
  • 2022-12-23
  • 2021-11-11
  • 2022-01-17
  • 2021-09-15
  • 2022-01-18
相关资源
相似解决方案