This one is more advanced indeed, here is how to use it so that the code is compatible with both PHP 5.5 and PHP 4.

First, instead of preg_replace() with arguments arrays, we use the built-in PPRA() function which will call intelligently preg_replace() or preg_replace_callback() for every element of the arrays:

  if ($pg) $txt = preg_replace( # before
  if ($pg) $txt = PPRA(         # after

First, the search pattern is written without the /e flag:

  '/\[\[[^\]]*?\bAttach:([^"\]\|]*)/e', # before
  '/\[\[[^\]]*?\bAttach:([^"\]\|]*)/',  # after

Second, the replace string is converted into a callback function:

  "'[['.AttachtableCountUploadLinks(\$pn,'','$1')", # before
PCCF("return '[['.AttachtableCountUploadLinks('$pn','',\$m[1]);"), # after

Note that we have wrapped the previous code in a line like
  PCCF("return PREVCODEHERE ;")

Also, two important changes:

The '$1' is written as \$m[1].

The \$pn variable is EMBEDDED as quoted expanded STRING "...'$pn'..." into the code to be evaluated, because as a local variable it would be out of the scope of the callback function, thus inaccessible.

So, the last part would probably look like this:

  PCCF("return '[['.AttachtableCountUploadLinks('$pn','',\$m[1]);"),
  PCCF("return AttachtableCountUploadLinks('$pn','',\$m[1]);"),
  PHSC( $pg['text'], ENT_NOQUOTES ) );

(untested, might need some tweaking)

PCCF="PmWiki Create Callback Function", see/improve
  http://www.pmwiki.org/wiki/PmWiki/Functions (documentation)
  http://www.pmwiki.org/wiki/Cookbook/ROSPatterns (example)

PPRA="PmWiki preg replace array", see/improve
  http://www.pmwiki.org/wiki/PmWiki/Functions (documentation)
  http://www.pmwiki.org/wiki/Cookbook/DotsInLinks (example, also PCCF)

Petko

On 09.12.2014 15:08, Hans Bracker wrote:
I  tried  to  update  the  attachtable.php  script, to make it PHP 5.5
compatible. But I could not get the following section (lines 174-186)
revise dfor PHP 5.5 compatibility:

foreach( $ls as $pn ) {
  $pg = RetrieveAuthPage( $pn, 'read', FALSE, READPAGE_CURRENT );
  if ($pg) $txt = preg_replace(
    array(
      "/(\n[^\\S\n]*)?\\[([=@])(.*?)\\2\\]/s",        ## preserved text
'/\[\[[^\]]*?\bAttach:([^"\]\|]*)/e', ## links to attachments

"/\\bAttach:([^\\s$UrlExcludeChars]*[^\\s.,?!$UrlExcludeChars])/e" ),
 ## raw attachments
   array(
      ' ',
      "'[['.AttachtableCountUploadLinks(\$pn,'','$1')",
      "AttachtableCountUploadLinks(\$pn,'','$1')" ),
      htmlspecialchars( $pg['text'], ENT_NOQUOTES ) );        ##
assumes $MarkupFrame[0]['escape'] == 1
}

How  can  this  be  rewritten?  preg_replace  with /e modifier is used
twice, and I don't know how to create correct preg_replace_callback
or use PPRA or PPRE instead. I tried in various ways and get warnings,
and  the  Attach:  markup in links or otherwise is not picked up (when
the list of pages gets larger than 8).


Best regards,
 Hans
mailto:[email protected]
www.softflow.co.uk


_______________________________________________
pmwiki-users mailing list
[email protected]
http://www.pmichaud.com/mailman/listinfo/pmwiki-users

_______________________________________________
pmwiki-users mailing list
[email protected]
http://www.pmichaud.com/mailman/listinfo/pmwiki-users

Reply via email to