当在一个类中用 preg_replace 碰上了 $this时,悲剧发生了,请高手帮帮忙。

striker_un 2009-12-22 04:09:28
<?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();

...全文
162 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
江南昆虫 2009-12-22
  • 打赏
  • 举报
回复
用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();
?>
foolbirdflyfirst 2009-12-22
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 xuzuning 的回复:]
不是回答你了吗?那句改成
$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']"
[/Quote]
lz给唠叨分,呵呵.
striker_un 2009-12-22
  • 打赏
  • 举报
回复
这么多高手回帖,感动啊。。

呵呵,又找出一种解决方法。 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();

?>

xuzuning 2009-12-22
  • 打赏
  • 举报
回复
不是回答你了吗?那句改成
$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']"
jlzan1314 2009-12-22
  • 打赏
  • 举报
回复
看你想干什么了? $要加\ 或者单引号括起
或者//ie 用单引号括起,运行那个字符串的值..都可以..
striker_un 2009-12-22
  • 打赏
  • 举报
回复
to 六楼:
不要做简单的排列组合,
基础的排列组合,你能想到的我全试过了。
striker_un 2009-12-22
  • 打赏
  • 举报
回复

/*********
奶奶的,
自己写了个单件模式才搞定。。
********/
<?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();
liuahuilele 2009-12-22
  • 打赏
  • 举报
回复
$model_content = preg_replace("/\[!--([^-]*)--\]/i",$this->config_arr[test1], $model_content);

这样就可以了
foolbirdflyfirst 2009-12-22
  • 打赏
  • 举报
回复
看来不能想当然。
这样可以

//请大家帮帮我,一个语法问题,非常感激
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();
foolbirdflyfirst 2009-12-22
  • 打赏
  • 举报
回复
我搞错了,
再看看。
xuzuning 2009-12-22
  • 打赏
  • 举报
回复
$model_content = preg_replace("/\[!--([^-]*)--\]/ie",'$this->config_arr["\\1"]', $model_content);
foolbirdflyfirst 2009-12-22
  • 打赏
  • 举报
回复
//请大家帮帮我,一个语法问题,非常感激
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();
cyxin2121921 2009-12-22
  • 打赏
  • 举报
回复
帮顶

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧