坐等高手,我不会找这样的规律。

解决方案 »

  1.   


    正解,结合使用join和preg_replace_callback即可,另外正则匹配也需要正确:join
    (PHP 4, PHP 5)join — 别名 implode()说明
    此函数是该函数的别名: implode(). implode
    (PHP 4, PHP 5)implode — Join array elements with a string说明
    string implode ( string $glue , array $pieces )
    string implode ( array $pieces )
    Join array elements with a glue string. Note: implode() can, for historical reasons, accept its parameters in either order. For consistency with explode(), however, it may be less confusing to use the documented order of arguments. 
    参数glue 
    Defaults to an empty string. This is not the preferred usage of implode() as glue would be the second parameter and thus, the bad prototype would be used. pieces 
    The array of strings to implode. 
    返回值
    Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element. 更新日志
    版本 说明 
    4.3.0 The glue parameter became optional.  
    范例Example #1 implode() example<?php$array = array('lastname', 'email', 'phone');
    $comma_separated = implode(",", $array);echo $comma_separated; // lastname,email,phone// Empty string when using an empty array:
    var_dump(implode('hello', array())); // string(0) ""?> 注释
    Note: 此函数可安全用于二进制对象。preg_replace_callback
    (PHP 4 >= 4.0.5, PHP 5)preg_replace_callback — 用回调函数执行正则表达式的搜索和替换说明
    mixed preg_replace_callback ( mixed $pattern , callback $callback , mixed $subject [, int $limit ] )
    本函数的行为几乎和 preg_replace() 一样,除了不是提供一个 replacement 参数,而是指定一个 callback 函数。该函数将以目标字符串中的匹配数组作为输入参数,并返回用于替换的字符串。 Example #1 preg_replace_callback() 例子<?php
      // 此文本是用于 2002 年的,
      // 现在想使其能用于 2003 年
      $text = "April fools day is 04/01/2002\n";
      $text.= "Last christmas was 12/24/2001\n";  // 回调函数
      function next_year($matches) {
        // 通常:$matches[0] 是完整的匹配项
        // $matches[1] 是第一个括号中的子模式的匹配项
        // 以此类推
        return $matches[1].($matches[2]+1);
      }  echo preg_replace_callback(
                  "|(\d{2}/\d{2}/)(\d{4})|",
                  "next_year",
                  $text);  // 结果为:
      // April fools day is 04/01/2003
      // Last christmas was 12/24/2002
    ?> 
    You'll often need the callback function for a preg_replace_callback() in just one place. In this case you can use create_function() to declare an anonymous function as callback within the call to preg_replace_callback(). By doing it this way you have all information for the call in one place and do not clutter the function namespace with a callback functions name not used anywhere else. Example #2 preg_replace_callback() 和 create_function()<?php
      /* 一个 UNIX 风格的命令行过滤器,将每个段落开头的
       * 大写字母转换成小写字母 */  $fp = fopen("php://stdin", "r") or die("can't read stdin");
      while (!feof($fp)) {
          $line = fgets($fp);
          $line = preg_replace_callback(
              '|<p>\s*\w|',
              create_function(
                  // 这里使用单引号很关键,
                  // 否则就把所有的 $ 换成 \$
                  '$matches',
                  'return strtolower($matches[0]);'
              ),
              $line
          );
          echo $line;
      }
      fclose($fp);
    ?>