<?
$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);
?>preg_replace -- Perform a regular expression search and replace
Description
mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit])
Searches subject for matches to pattern and replaces them with replacement. If limit is specified, then only limit matches will be replaced; if limit is omitted or is -1, then all matches are replaced. Replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n'th parenthesized pattern. n can be from 0 to 99, and \\0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. 注: When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar \\1 notation for your backreference. \\11, for example, would confuse preg_replace() since it does not know whether you want the \\1 backreference followed by a literal 1, or the \\11 backreference followed by nothing. In this case the solution is to use \${1}1. This creates an isolated $1 backreference, leaving the 1 as a literal. 

解决方案 »

  1.   

    indeed(indeed) 的例子不能解决我的问题
      

  2.   

    <?php
    $s = "English-Chinese Dictionary. English: Books for Learning Chinese available at Amazon.com
    Click here to learn Chinese Sayings online Learn Chinese the fun way by taking the Chinese
    Quiz online.
    ";preg_match_all("/.+Chinese/U",$s,$regs);
    $p = $regs[0];foreach($p as $k=>$v) {
      $p[$k] = "/$v/";
      $r[$k] = $v."_".($k+1);
    }
    echo preg_replace($p,$r,$s);
    ?>
    输出
    English-Chinese_1 Dictionary. English: Books for Learning Chinese_2 available at Amazon.com
    Click here to learn Chinese_3 Sayings online Learn Chinese_4 the fun way by taking the Chinese_5
    Quiz online.
      

  3.   

    xuzuning(唠叨) 你的方法基本能满足要求,但还有一个没有解决
    如果源字符串是
    English-Chinese Dictionary. English: Books for Learning Chinese available at Amazon.com Click here to learn Chinese Sayings online Learn Chinese the fun way by taking the Chinese Quiz online ChineseChinese. 
    用你的方法替换后为
    English-Chinese_1 Dictionary. English: Books for Learning Chinese_2 available at Amazon.com Click here to learn Chinese_3 Sayings online Learn Chinese_4 the fun way by taking the Chinese_5 Quiz online Chinese_6Chinese. 
    而我想要的是:
    English-Chinese_1 Dictionary. English: Books for Learning Chinese_2 available at Amazon.com Click here to learn Chinese_3 Sayings online Learn Chinese_4 the fun way by taking the Chinese_5 Quiz online Chinese_6Chinese_7. 是最后一个单词的区别~
    你再看看这个问题,谢了!
      

  4.   

    是的,不可能用一个“形式语言”的模板去匹配“自然语言”各种形态。能匹配的只是符合某种规律的子集。比如ChineseChinese显然不符合英文印刷规范。如果在其间加一空格,就可以了。
      

  5.   

    另外,如果你使用贪婪模式的php正则表达式,情况可能会更好。
    ereg("Chinese",$str,$regs)将每次匹配最后一个Chinese
      

  6.   

    唠叨看看这个方法怎么样
    <?php
    $ss=
    "English-Chinese Dictionary. English: Books for Learning Chinese available at Amazon.com
    Click here to learn Chinese Sayings online Learn Chinese the fun way 
    by taking the Chinese Quiz online. ChineseChinese";
    echo $ss."<br><br>";$arr=explode("Chinese",$ss);
    if (count($arr)==1) $ss=$arr[0];
    else {
        $ss="";
        for ($i=0;$i<(count($arr)-1);$i++) {
            $ss.=$arr[$i]."Chinese_".($i+1);
        }
        $ss.=$arr[count($arr)-1];
    }echo $ss."<br>";?>