<?php
$find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr));
?>输出:Array
(
[0] => B
[1] =>
[2] => !
)为什么 会输出这样的内容?

解决方案 »

  1.   

    不是你让它把hello world替换成b的嘛
    找/* if subject is an array */的几个注释,看看它面对值是array的时候,如何处理的。
    PHP_FUNCTION(str_replace)
    {
        php_str_replace_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
    }static void php_str_replace_common(INTERNAL_FUNCTION_PARAMETERS, int case_sensitivity)
    {
        zval **subject, **search, **replace, **subject_entry, **zcount = NULL;
        zval *result;
        char *string_key;
        uint string_key_len;
        ulong num_key;
        int count = 0;
        int argc = ZEND_NUM_ARGS();    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ZZZ|Z", &search, &replace, &subject, &zcount) == FAILURE) {
            return;
        }    SEPARATE_ZVAL(search);
        SEPARATE_ZVAL(replace);
        SEPARATE_ZVAL(subject);    /* Make sure we're dealing with strings and do the replacement. */
        if (Z_TYPE_PP(search) != IS_ARRAY) {
            convert_to_string_ex(search);
            convert_to_string_ex(replace);
        } else if (Z_TYPE_PP(replace) != IS_ARRAY) {
            convert_to_string_ex(replace);
        }    /* if subject is an array */
    if (Z_TYPE_PP(subject) == IS_ARRAY) {
            array_init(return_value);
            zend_hash_internal_pointer_reset(Z_ARRVAL_PP(subject));        /* For each subject entry, convert it to string, then perform replacement
               and add the result to the return_value array. */
            while (zend_hash_get_current_data(Z_ARRVAL_PP(subject), (void **)&subject_entry) == SUCCESS) {
                if (Z_TYPE_PP(subject_entry) != IS_ARRAY && Z_TYPE_PP(subject_entry) != IS_OBJECT) {
                    MAKE_STD_ZVAL(result);
                    SEPARATE_ZVAL(subject_entry);
                    php_str_replace_in_subject(*search, *replace, subject_entry, result, case_sensitivity, (argc > 3) ? &count : NULL);
                } else {
                    ALLOC_ZVAL(result);
                    Z_ADDREF_P(*subject_entry);
                    COPY_PZVAL_TO_ZVAL(*result, *subject_entry);
                }
                /* Add to return array */
                switch (zend_hash_get_current_key_ex(Z_ARRVAL_PP(subject), &string_key,
                                                    &string_key_len, &num_key, 0, NULL)) {
                    case HASH_KEY_IS_STRING:
                        add_assoc_zval_ex(return_value, string_key, string_key_len, result);
                        break;                case HASH_KEY_IS_LONG:
                        add_index_zval(return_value, num_key, result);
                        break;
                }            zend_hash_move_forward(Z_ARRVAL_PP(subject));
            }
        } else {    /* if subject is not an array */
            php_str_replace_in_subject(*search, *replace, subject, return_value, case_sensitivity, (argc > 3) ? &count : NULL);
        }
        if (argc > 3) {
            zval_dtor(*zcount);
            ZVAL_LONG(*zcount, count);
        }
    }
      

  2.   

    mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count] )
    在 subject 中,将 search 替换为 replace你有
    $find = array("Hello","world");
    $replace = array("B");
    所以
    Hello 被替换成 B
    world 被替换成“空”,因为$replace中没有对应项还有
    $arr = array("Hello","world","!");
    即来源是数组,替换是按每个数组成员依次进行的
    成员"!"没有对应的替换规则,所以不会被替换
      

  3.   

    普通的字符替换倒是明白。就是数组 array("B") 这个数组中的 “B” 被当视为一个什么类型去 aray("hello","word") 这个数组中替换的 
    按正常的字符替换去理解的话,“hello”,“word” 中并没有 “B” 这个字符.还是数给之间的 搜索替换 方式跟 普通的字符 搜索替换方式不一样.  
      

  4.   

    紧跟回贴。哦 明白了 原来 数组之间的替换搜索 是通过 数组元素个数去  匹配替换的array("hello","word")两个元素 array("B")一个元素,匹配的时候 array("hello","word")只在这个数给里面去匹配一次替换,因为array("B")只有一个元素是这样的吧。
      

  5.   

    因为array("B")这个数组中只有一个有值元素,所以array("hello","word")中的第二个元素就被换成空值了多谢版主 的这一句 “world 被替换成“空”,因为$replace中没有对应项” 忽然明白.
      

  6.   

    str_replace($find,$replace,$arr)
    楼主,请吧这个函数的三个参数记得,第一个参数是被替换的字符,第二个是替换的参数(即替换后的参数),第三个是被替换的信息。举个栗子
    例子 1
    <?php
    echo str_replace("world","John","Hello world!");
    ?>
    输出:
    Hello John!
    例子 2
    在本例中,我们将演示带有数组和 count 变量的 str_replace() 函数:
    <?php
    $arr = array("blue","red","green","yellow");
    print_r(str_replace("red","pink",$arr,$i));
    echo "Replacements: $i";
    ?>
    输出:
    Array
    (
    [0] => blue
    [1] => pink
    [2] => green
    [3] => yellow
    )
    Replacements: 1
    例子 3
    <?php
    $find = array("Hello","world");
    $replace = array("B");
    $arr = array("Hello","world","!");
    print_r(str_replace($find,$replace,$arr));
    ?>
    输出:
    Array
    (
    [0] => B
    [1] =>
    [2] => !
    )
      

  7.   

    因为你替换的是数组、、请在print_r(str_replace($find,$replace,$arr));改成print_r(str_replace($find[这里为数组位置],$replace[这里为数组位置],$arr[这里为数组位置]));
      

  8.   

    <?php
    $find = array("Hello","world");
    $replace = array("B");
    $arr = array("Hello","world","!");
    print_r(str_replace($find,$replace,$arr));
    ?>
    分析下,二维数组("Hello","world")替换成("B")
    我们可以认为 $replace = array("B","");
    所以"Hello" => "B"  "world"=>""
    替换后array("Hello","world","!"); =? array("B","","!");