<?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();
//请大家帮帮我,一个语法问题,非常感激
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();
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();
这样可以//请大家帮帮我,一个语法问题,非常感激
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();
/*********
奶奶的,
自己写了个单件模式才搞定。。
********/
<?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();
$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']"
$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();?>
//请大家帮帮我,一个语法问题,非常感激
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();
?>