views:

19

answers:

1

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', '', 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

+2  A: 

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)

mvds
instead of get_CI(), just pass by reference as it says in the user guide, like so: $CI =
Matthew
Looks like you're right, but since I did't know what else get_instance() does, I figured I'd better stay on the safe side ;-)
mvds
Calle
updated my answer to reflect this. You're welcome!
mvds