-1

Here is the PHP code before deprecation errors were introduced:

array_walk( $tags, 'shortcode_tag_parse', &$content );

function shortcode_tag_parse( &$tag, $key, &$content ) {

    $content = preg_replace_callback( "{\[{$tag}[^\]]*\].*?\[/{$tag}\]}is", 'shortcode_img_unautop', $content );

}

// Replace <p> tags around images
function shortcode_img_unautop( $match ) {

    return preg_replace( '#\s*<p>\s*(<a .*>)?\s*(<img .*>)\s*(<\/a>)?\s*<\/p>\s*#iU', '\1\2\3', $match[0] );

}

Now if I remove the call-time reference:

array_walk( $tags, 'shortcode_tag_parse', $content );

It no longer modifies $content, and I absolutely need to do this. I will not settle for switching to a loop. The other topics I've looked at simply say to remove the call-time reference &, and when I did that, it no longer does what it was supposed to do.

Does anyone know how I can get my code to work again, without the need for looping (array_walk and array_map look better for me).

Charles
  • 50,943
  • 13
  • 104
  • 142
Nahydrin
  • 13,197
  • 12
  • 59
  • 101
  • You can implement your own version of array_walk with your own definition. – Mike B Nov 21 '13 at 16:49
  • It looks like you're implementing a BBCode parser. This is a solved problem and should probably use third-party library code if you can instead of rolling your own. – Charles Nov 21 '13 at 17:06
  • @Charles It's not parsing BBCODE, it's just stripping

    tags that are found around images between bbcode/shortcode. WordPress' built-in parser is still being used. The `$tags` are from WordPress' own shortcode regex that it uses, so I know they exist and have correct end tags.

    – Nahydrin Nov 21 '13 at 17:50

1 Answers1

0

As mentioned in the comments, your best bet here is a reimplementation of array_walk that expects the last parameter to be a reference. Call-time pass-by-ref to builtins is dangerous and was basically universally removed after some pretty harsh security exploits.

Here's an entirely untested example.

function array_walk_userdata_by_ref(array &$array, $callback, &$userdata = null) {
    foreach($array as $k => $v) {
        $array[$k] = $callback( $v, $k, $userdata );
    }
}
Charles
  • 50,943
  • 13
  • 104
  • 142