Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QUESTION Using (%%)
#12
(2019-04-26, 02:46:42)shawn_a Wrote: This is what I wrote to do this

PHP Code:
function getExpansion($trigger,$contents,$noargs false){
 
 // todo: get lookaheads and lookbehind implemented so you can put non executing code in pages for examples.
 
 
  
// $lookb = "<code>";
 
 // $looka = "</code>";
 
 
  
// $pattern = "/(?<!".$lookb.")\(%\s*(".$trigger.")(\s+(?:%[^%\)]|[^%])+)?\s*%\)(?!".$looka.")/";
 
 // $pattern = "/(?<=".$look.")\(%\s*(".$trigger.")(\s+(?:%[^%\)]|[^%])+)?\s*%\)(?=".$look.")/";
 
 $pattern "/\(%\s*(".$trigger.")(\s+(?:%[^%\)]|[^%])+)?\s*%\)/";
 
 
  if
(preg_match($pattern,$contents,$matches)){
 
   # echo "<h2>Trigger was Found</h2>"; print_r($matches);
 
   if(!empty($matches[2]) && !$noargs){
 
     // get arguments
 
     # echo "<h2>Trigger has arguments</h2>";      
 
     return array(true,'args'=>get_args(trim($matches[2])) );
 
   }
 
   else
    
{
 
     return array(true);
 
   }
 
 }
 
 
}

function 
get_args($str){
 
 /*
    Gets key value pairs from a string
    pairs are deimited by space
    values can be defined as either
      key=value_no_spaces
      key='value spaces'
      key = "value spaces"
      key
     * spaces in the pair assigment do not matter
  */
 
 
  
// TODO: make delimiters variable
 
 $regex = <<<EOD
  /(?J)(?:(?P<key>\w+)\s*\=\s*["'](?P<value>[^"']*(?:["']{2}[^"']*)*)["']) | (?:(?P<key>\w+)\s*\=\s*(?<value>[^"'\s]*)) | (?:(?P<key>\w+)\s*)/ix
EOD;
 
 
  $args 
= Array();
 
 
  preg_match_all
($regex,$str,$argsraw,PREG_SET_ORDER);
 
 
  if
($argsraw){
 
   $cnt count($argsraw);
 
   for($i=0;$i<$cnt;$i++){
 
     # echo $argsraw[$i]['key'] . " = " . $argsraw[$i]['value'] . "<br>";
 
     $args[strtolower($argsraw[$i]['key'])] = $argsraw[$i]['value'];
 
   }
 
  
  
  return $args
;
  

Nice one. I have to admit that even after several years coding, I still don't utilise REGEX half as much as I should.
Reply


Messages In This Thread
Using (%%) - by craiga - 2019-02-23, 22:58:57
RE: Using (%%) - by datiswous - 2019-02-27, 23:10:42
RE: Using (%%) - by craiga - 2019-03-01, 22:49:39
RE: Using (%%) - by craiga - 2019-04-23, 00:14:43
RE: Using (%%) - by datiswous - 2019-04-23, 06:56:30
RE: Using (%%) - by craiga - 2019-04-23, 08:05:04
RE: Using (%%) - by smdp-1971 - 2019-04-23, 08:29:54
RE: Using (%%) - by craiga - 2019-04-23, 17:18:40
RE: Using (%%) - by smdp-1971 - 2019-04-23, 22:11:38
RE: Using (%%) - by craiga - 2019-04-23, 20:48:06
RE: Using (%%) - by shawn_a - 2019-04-26, 02:46:42
RE: Using (%%) - by craiga - 2019-04-29, 18:50:02
RE: Using (%%) - by shawn_a - 2019-05-04, 08:51:08



Users browsing this thread: 1 Guest(s)