You are here: Re: [PHP] Dynamically loading function arguments « PHP « IT news, forums, messages
Re: [PHP] Dynamically loading function arguments

Posted by Jochem Maas on 10/21/07 11:17

RaTT wrote:
> Hi
>
> Currently we are building a basic content management system where we
> would like to be able to write as libral customized tags as possible,
>
> for example [[select:array_name,title]] or [[form:name]] in the
> format of [[<function>:<arguments>]]
>
> The problem that we are experiencing are that the arguments are being
> parsed as a single string, does anybody know of what function / method
> i can use to get around this? My default option is to write the
> functions to split the single supplied argument into their respective
> arguments, but i am sure there is a easier way around?
>
> I have tried splitting them up, and rejoining them with a "," but i
> think i am just redoing what php does internally.

some funcs that might help you:

compact()
extract()
call_user_func()
call_user_func_array()

also I recommend doing everything you can not to have to use
an eval() statement anywhere with this code as that would be comparatively
slow and you have to start worrying about possible security issues...

lastly my personal preference is towards the preg_*() funcs
rather than the eregi_*() funcs. regardless you should minimize the use
of regular expressions if you can... use explode(), strstr() and the like
where ever possible.

if you find a neat way of making use of call_user_func_array() then I think
you can cut the step where you rebuild the params string with commas.

also you might want to check out the way the Smarty guys implemented their
plugin/tag functionality - you may not like their style or implementation
but no doubt there is some overlap in what you are trying to do and what
they have done - so it may give you some ideas.

good luck.

>
> Here is my current code,
> <?php
>
> // function = select, array = _title_, name= select_title
>
> $str = "{{select:_title_,select_title}}";
>
> parse($str);
>
> function parse($str){
> $reg_ex = '/\{\{([a-zA-Z0-9\-_]+):(.*)\}\}/'; //
> {{function_name:paramaters,seperate,by,comma}}
> preg_match_all($reg_ex,$str,$matches,PREG_SET_ORDER);
> // $matches[0] = whole function
> // $matches[1] = function
> // $matches[2] = content / params
>
> echo $match_count = count($matches);
> for ($i=0;$i<$match_count;$i++){
> $output = null;
> if(function_exists($matches[$i][1])){
> //function has been declared
> // TO-DO: add safe mode function array to specify allowed functions
> #echo " DEBUG: {$matches[$i][1]} ( {$matches[$i][2]} ) <br
> />\n";
> $args = explode(",",$matches[$i][2]);
> $cnt = count($args);
> $params = null;
> for($j=0; $j < $cnt; $j++){
> #$params .= "\$args[$j]";
> $params .= $args[$j];
> $params .= ($j != ($cnt-1))? ",": "";
> }// end for
> //eval("echo $params;");
> //$output =
> $matches[$i][1](implode(",",$args));//single str
> $output = $matches[$i][1]($params);
> $str = eregi_replace($matches[$i][0],$output,$str);
> } //end if
> else {
> // function not found or allowed remove tags
> //echo "DEBUG: function not found<br />\n";
> $str = eregi_replace($matches[$i][0],'',$str);
> }// end else
> }//end for
> return $str;
> }
>
> function select($array='',$name='',$extra=''){
> echo '<pre>';
> print_r(func_get_args());
> echo '</pre>';
> }
> ?>
>
> Kind regards
> Jarratt
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация