Preg_replace with array replacements

前端 未结 4 1195
说谎
说谎 2020-12-03 07:50
$string = \":abc and :def have apples.\";
$replacements = array(\'Mary\', \'Jane\');

should become:

Mary and Jane have apples.
         


        
4条回答
  •  Happy的楠姐
    2020-12-03 08:43

    Try this

    $to_replace = array(':abc', ':def', ':ghi');
    $replace_with = array('Marry', 'Jane', 'Bob');
    
    $string = ":abc and :def have apples, but :ghi doesn't";
    
    $string = strtr($string, array_combine($to_replace, $replace_with));
    echo $string;
    

    here is result: http://sandbox.onlinephpfunctions.com/code/7a4c5b00f68ec40fdb35ce189d26446e3a2501c2

提交回复
热议问题