> John Rankin writes: >> On 8/01/14 12:31 PM, Petko Yotov wrote: >>> Currently the PmWiki core does not restrict you at all to use closures >>> in >>> your recipes. You can have them, they will only work on PHP 5.3+ sites. >> That works for me :-) >> >> However, some advice on how best to convert the following case would be >> helpful. I use some code to extract the text of a link in the form: >> >> $LinkTidy = array("/reg-exp1/" => >> "MakeLink(\$pagename, ... matches ... , '\$LinkText')", >> "/reg-exp2/" => >> "MakeLink(\$pagename, ... matches ... , '\$LinkText')"); >> foreach ($LinkTidy as $exp => $repl) { >> $text = SomeFunction($exp, $repl, $text); >> } >> >> Currently, SomeFunction is preg_replace, $exp has an e modifier and >> matches >> contains '$1', '$2', etc as appropriate to the $exp. In some cases, $exp >> has >> no modifier and $repl is simply '$1'. >> >> What is the best way to convert this? Preferably without a major code >> re- >> write, as I use this construct in several places. > > For PHP 5.5 you need to create callback functions where your replacement > requires evaluation. > > And, you need to pass the $pagename value to the callback function. > > Here is how I would try this: > > ... > > 1. Instead of '$0', '$1', '$2', '$3' ... we have $m[0], $m[1], $m[2], > $m[3], > no single quotes, and as we don't want to expand them when we declare the > callback, we precede them with backslash: \$m[0], \$m[1], \$m[2], \$m[3]. > > 2. $pagename is not in the scope of the callback function, so we don't > prefix it with \ and we wrap it in single quotes : > > "return MakeLink('$pagename',...);" > > this will expand $pagename and its "value" will be written as a string > into > the callback. This assumes that at the moment when you define the callback > you know the value of $pagename, like in your example.
Sorry, I over-simplified. In practice, the $LinkTidy array is defined once at the start and then referenced as a global variable several times in different places to do the actual tidying. So at the time $LinkTidy is defined, the code may not know the $pagename. Potentially, the tidying can apply to multiple different $pagename values as it assembles several wiki pages into one output. Do I use \$pagename instead? > > Read the function PCCF(), you can define custom callback templates and > thus > write less code. > > Petko > > JR -- John Rankin _______________________________________________ pmwiki-users mailing list pmwiki-users@pmichaud.com http://www.pmichaud.com/mailman/listinfo/pmwiki-users