<?php
$string = "The quick brown fox jumped over the lazy dog.";$patterns[0] = "/quick/";
$patterns[1] = "/brown/";
$patterns[2] = "/fox/";$replacements[2] = "bear";
$replacements[1] = "black";
$replacements[0] = "slow";print preg_replace($patterns, $replacements, $string);/* Output
   ======The bear black slow jumped over the lazy dog.*//* By ksorting patterns and replacements,
   we should get what we wanted. */ksort($patterns);
ksort($replacements);print preg_replace($patterns, $replacements, $string);/* Output
   ======The slow black bear jumped over the lazy dog.*/?>

解决方案 »

  1.   


    $default = "I think the most interesting food is {food},so I must have 
    lots of {what} to enjoy it";
    $Array['food'] = 'rice';
    $Array['what'] = 'money';
    function Array($t){
    global $Array;
    return $Array[$t];
    }
    echo preg_replace("/\{(.*?)\}/ei","Array('\\1')",$default);
      

  2.   

    I think the most interesting food is {food},so I must have lots of {what} to enjoy it
    ==>
    I think the most interesting food is {rice},so I must have lots of {money} to enjoy it
    ???
    $default = "I think the most interesting food is {food},so I must have 
    lots of {what} to enjoy it";
    $patterns = array(
    "/\{food\}/si",
    "/\{what\}/si"
    );
    $Array = array(
    "{rice}",
    "{money}"
    );
    echo preg_replace($patterns,$Array,$default);