当大于十时,使用str_replace作为数字

时间:2015-04-01 11:02:23

标签: php

这是我的代码 -

$input=10;
$number = array('0','1','2','3','4','5','6','7','8','9','10');
$text = array('a','b','c','d','e','f','g','h','i','j','k');
$print = str_replace($number, $text, $input);

我期待的是用'k'替换'10'。但它取代10为'ba' 在上述条件下如何实际使用str_replace?

2 个答案:

答案 0 :(得分:1)

试试这个..

    $input=10;
    $number = array('0','1','2','3','4','5','6','7','8','9','10');
    $text = array('a','b','c','d','e','f','g','h','i','j','k');

    $key=array_search($input,$number);
    $number[$key]='k';
    print_r($number);

(或)

$number[$key]=$text[$key];
print_r($number);

输出:

Array( 
        [0] => 0 
        [1] => 1
        [2] => 2
        [3] => 3
        [4] => 4
        [5] => 5
        [6] => 6 
        [7] => 7 
        [8] => 8 
        [9] => 9 
        [10] => k
     )

答案 1 :(得分:0)

$input=10;
$number = array('10','0','1','2','3','4','5','6','7','8','9');
$text = array('k','a','b','c','d','e','f','g','h','i','j');
$print = str_replace($number, $text, $input);

足以放置' 10'和' k'在开始时,所以他首先替换这个字符串,而不是任何其他字符串。