[pmwiki-users] How to pass markup values to function . . .

Knut Alboldt pmwiki at alboldt.de
Tue Feb 8 11:33:39 CST 2005


Ben Wilson schrieb:
> I have a markup, such as:
> 
> (:thismarkup key=value:)
> 
> And I want to pass the key=value to a function where the values can
> the be put into form fields. How can I pass the values?
> 

how about this (I've put that into config.php):

function GetDirectiveArgs($args,$opt=array())
{
   # adopted from PITS.php
   # parse passed string into single arguments
   # arg-format: results in:
   # "arg1"               $arg['arg1'] = 1
   # "+arg2"              $arg['arg2'] = 1
   # "-arg3"              $arg['arg3'] = 0
   # "arg4=string"        $arg['arg4'] = 'string'
   # "arg5=string_string" $arg['arg5'] = 'string string'
   #
   # default values can be passed in the second parameter (as an array)
   # sample call see below

   $terms = preg_split('/((?<!\\S)[-+]?[\'"].*?[\'"](?!\\S)|\\S+)/',
     $args,-1,PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);

   # @DEBUG(print_r($terms,true),1);

   foreach($terms as $t) {
     if (trim($t)=='') continue;
     if (preg_match('/([^\'":=]*)[:=]([\'"]?)(.*?)\\2$/',$t,$match))
       $opt[$match[1]] = str_replace("_"," ",$match[3]);
     else
     {
       if (substr($t,0,1)=='-')
         { $opt[substr($t,1)] = 0; }
       elseif (substr($t,0,1)=='+')
         { $opt[substr($t,1)] = 1; }
       else
         { $opt[$t] = 1;  }
     }
   }

   return $opt;
}

# for testing use:
#  (:testgetarg arg1 arg2=value2 arg3=value_with_virtual_blanks +arg4 
-arg5 :)

Markup('testgetarg','testgetarg','/\\(:testgetarg\\s*(.*?)\\s*:\\)/e','TestGetArg("$1")');

function TestgetArg($args)
{
   $opt['default'] = 'DEFAULT';
   $opt['arg1']    = 'DEFAULT ARG1';
   $arglist = GetDirectiveArgs($args,$opt);
   echo "<pre>".print_r($arglist,true)."</pre>";
}

Knut




More information about the pmwiki-users mailing list