<?php
//请大家帮帮我,一个语法问题,非常感激
class text {
private $config_arr = array();

function __construct() {
   $this->config_arr  = array("test1"=>"t1","test2"=>"t2");
}

function index()
{
$model_content = "fdsafdsafdsa[!--test1--]f fdsafdsa [!--test2--]sd";

//我希望能将 [!--test1--] 替换成$this->config_arr["test1"] 最终值为 t1
$model_content = preg_replace("/\[!--([^-]*)--\]/i","$this->config_arr['\\1']", $model_content);

echo $model_content;
// the output is   fdsafdsafdsaArray['test1']f fdsafdsa Array['test2']sd
}

}
$me = new text();
$me->index();

解决方案 »

  1.   

    //请大家帮帮我,一个语法问题,非常感激
    class text {
        private $config_arr = array();
        
        function __construct() {
           $this->config_arr  = array("test1"=>"t1","test2"=>"t2");
        }
        
        function index()
        {
            $model_content = "a[!--test1--] b[!--test2--]c";
            
            //我希望能将 [!--test1--] 替换成$this->config_arr["test1"] 最终值为 t1
            $model_content = preg_replace("/\[!--([^-]*)--\]/ie","$this->get('\\1')", $model_content);
            
            echo $model_content;
            // the output is   fdsafdsafdsaArray['test1']f fdsafdsa Array['test2']sd
        } function get($key){
    return $this->config_arr[$key];
    }
        
    }
    $me = new text();
    $me->index();
      

  2.   

    $model_content = preg_replace("/\[!--([^-]*)--\]/ie",'$this->config_arr["\\1"]', $model_content);
      

  3.   

    看来不能想当然。
    这样可以//请大家帮帮我,一个语法问题,非常感激
    class text {
        private $config_arr = array();
        
        function __construct() {
           $this->config_arr  = array("test1"=>"t1","test2"=>"t2");
        }
        
        function index()
        {
            $model_content = "a[!--test1--] b[!--test2--]c";
            
            //我希望能将 [!--test1--] 替换成$this->config_arr["test1"] 最终值为 t1
            $model_content = preg_replace("/\[!--([^-]*)--\]/ie","text::get('\\1')", $model_content);
            
            echo $model_content;
            // the output is   fdsafdsafdsaArray['test1']f fdsafdsa Array['test2']sd
        } function get($key){
    return $this->config_arr[$key];
    }
        
    }
    $me = new text();
    $me->index();
      

  4.   

    $model_content = preg_replace("/\[!--([^-]*)--\]/i",$this->config_arr[test1], $model_content);这样就可以了
      

  5.   


    /*********
    奶奶的,
    自己写了个单件模式才搞定。。
    ********/
    <?php
    class text {
        private static $instance;  
    public $config_arr = array();
    private function __construct() {  }  
        
        public static function getInstance() 
        { 
            if(self::$instance == null) 
            { 
                self::$instance = new text(); 
            }  
            return self::$instance; 
        } 
        
        public function load_config($arr){
         $this->config_arr  = $arr;
        }function index()
    {    $model_content = "fdsafdsafdsa[!--test1--]f fdsafdsa [!--test2--]sd";
    $mode = '#\[\!\-\-(.+)\-\-\]#Uise';
    $model_content = preg_replace($mode,"r('\\1')",$model_content);
    echo $model_content;}
    } //end of class text//外部函数,用于获取text 类的config_arr;
    function r($key)
    {
    $objA = text::getInstance();return $objA->config_arr[$key];
    }$me = text::getInstance();
    $config_arr = array("test1"=>"<font color=red>t1</font>","test2"=>"<font color=green>t2</font>");
    $me->load_config($config_arr);
    $me->index();
      

  6.   

    不是回答你了吗?那句改成
    $model_content = preg_replace("/\[!--([^-]*)--\]/ie",'$this->config_arr["\\1"]', $model_content); 
    即可
    1、必须要用 e 模式,否则不会做计算
    2、你原来的 "$this->config_arr['\\1']" 应写作 '$this->config_arr["\\1"]',也可写作 "\$this->config_arr['\\1']"
      

  7.   

    这么多高手回帖,感动啊。。呵呵,又找出一种解决方法。 array(&$this,'r') 可以指向到 $this->r 方法
    $model_content = preg_replace_callback($mode,array(&$this,'r'),$this->model_content);<?php
    class test
    {
    var $config_arr;
    var $model_content;function test()
    {
    $this->config_arr['q'] = 'qq';
    $this->config_arr['c'] = 'cc';
    $this->model_content = '111[!--c--]222[!--q--] ';
    }
    function t()
    {
    $mode = '#\[\!\-\-(.+)\-\-\]#Uis';
    $model_content = preg_replace_callback($mode,array(&$this,'r'),$this->model_content);
    echo $model_content;
    }
    function r($arr)
    {
    $key = $arr['1'];
    return $this->config_arr[$key];
    }
    }$a = new test();
    $a->t();?>
      

  8.   

    用preg_replace_callback吧:<?php
    //请大家帮帮我,一个语法问题,非常感激
    class text {
        private $config_arr = array();
        
        function __construct() {
           $this->config_arr  = array("test1"=>"t1","test2"=>"t2");
        }
        
        function index()
        {
            $model_content = "a[!--test1--] b[!--test2--]c";
            //我希望能将 [!--test1--] 替换成$this->config_arr["test1"] 最终值为 t1
            $model_content = preg_replace_callback("/\[!--([^-]*)--\]/i",array($this,'get'), $model_content);
            echo $model_content;
        }
        function get($matches){
            return $this->config_arr[$matches[1]];
        }
    }
    $me = new text();
    $me->index();
    ?>