preg_replace with views in Codeigniter, how?
I'm trying to parse a view with BBCode, and it works fine. But there is one feature I don't know how to implement.
[box=test] should be replaced with $this->load->view('admin/news/test', '开发者_JAVA技巧', true);
This is my code so far:
$CI =& get_instance();
$view = preg_replace("'\[box=(.*?)\]'i", "\\1", $str);
The thing here is that \1 will be the name of the view I want to load. Ideally, I'd want to do something like this:
$CI =& get_instance();
$str = preg_replace("'\[box=(.*?)\]'i", $CI->load->view('admin/news/'."\\1", '', true), $str);
return $str;
So hopefully you understand from this example what I am trying to do. But I don't have any clue how to really do it?
thanks
You could try:
$str = preg_replace_callback("'\[box=(.*?)\]'i",'myCallBack',$str);
function myCallBack($match)
{
$CI =& get_instance();
return $CI->load->view('admin/news/'.$match[1], '', true);
}
edit the pain in these callback issues always is the scope of the function; so you have to get $CI
from somewhere, in this case from get_instance() (which is always better than using a global variable)
精彩评论