dongtangu8403 2019-01-02 05:06
浏览 85
已采纳

PHP:如何用另一个数组中的一个替换数组中的每个字符

I want to replace each character from an array with one in another array:

<?php
$str="a b c d e f g h i j ";

$pattern=array();
$pattern[0]="a";
$pattern[1]="b";
$pattern[2]="c";
$pattern[3]="d";
$pattern[4]="e";
$pattern[5]="f";
$pattern[6]="g";
$pattern[7]="h";
$pattern[8]="i";
$pattern[9]="j";

$replacement=array();
$replacement[0]="6a";
$replacement[1]="6e";
$replacement[2]="6i";
$replacement[3]="6o";
$replacement[4]="6u";
$replacement[5]="5a";
$replacement[6]="5e";
$replacement[7]="5i";
$replacement[8]="5o";
$replacement[9]="5u";

echo str_replace($pattern,$replacement,$str);?>

RESULT:

    6a 66u 65o 6o 6u 5a 5e 55o 5o 5u
    a   b   c   d  e  f  g  h   i  j

the 'b' 'c' 'h' kind conflict with the others

  • 写回答

3条回答 默认 最新

  • duanpingzu7194 2019-01-02 06:46
    关注

    When using str_replace with arrays() as $search and $replace parameters, the function will evaluate every items in the $search array, for EACH characters of the $subject string. This is important to understand as every "loop" for a given character evaluates the content of the position of the $subject that it is working on at that point.

    This results in the function being able to change that character multiple times in one pass (that character from the $subject, against all the items in the $search array.)

    A simple example would be the following code/output:

    $str = "a b c";
    $pattern = array("a","b","a");
    $replacement = array("a1","b1","c1");
    echo str_replace($pattern, $replacement, $str); // Output : c11 b1 c
    

    The function executes the "search" against ALL items in the search array, for each character in $str. Here is the first "loop" for the first character in $str ("a") :

    First is finds "a", which gets replaced with "a1", then it looks for "b", which is not found. Then it looks for "a" again, and replaces the aforementioned replacement ("a"=>"a1") with the mapping "a"=>"c1" which leads to "c11".

    Then goes on to the next character in $str.

    YOUR EXAMPLE

    When the second character in your $str ("b") is searched-against, it gets replaced with "6e" (this is the new state of $str) then following the "loop" that NEW "e" is found in the $search array, and replaced with "6u". At that point, you have "6a 66u". You can extrapolate the rest.

    The reason why the first "a"=>"6a" is exact, is that the NEW state of $str after first iteration of the search "loop", that is "6a", will not match any of the other items in the search array.

    As per php documentation ( php.net str_replace() ) :

    Caution: Replacement order gotcha. Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements.

    This behavior has led to very interesting/unexpected results and was not trivial to debug.

    Some answers already point to solutions, I wanted to chime-in to provide details about the experienced behavior of php.

    Hope this helps, let me know! Cheers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?