preg_replace to preg_replace_callback with an array as replacement

Lucas

I'm migrating an application to work with PHP 5.5, and I need to remove the /e character from the preg_replace function, to do that I'm using preg_replace_callback()

My actual function looks like this:

preg_replace ( '#\{([a-z0-9\-_]*?)\}#Ssie' , '( ( isset($array[\'\1\']) ) ? $array[\'\1\'] : \'\' );' , $template );

Where:

$template contains a html document with tags like this one: {user_name}

and $array contains

$array['user_name'] = 'The user';

I've been trying to convert this to work with PHP 5.5 with not success.

This is what I did so far:

return preg_replace_callback ( '#\{([a-z0-9\-_]*?)\}#Ssi' , function ( $array ) {
    return ( ( isset ( $array[1] ) ) ? $array[1] : '' );
} , $template );

But It's not working. The tags closed in braces are not being replaced.

What Am I missing?

fire

You need to use the use keyword to pull in $array into your anonymous function...

return preg_replace_callback ( '#\{([a-z0-9\-_]*?)\}#Ssi' , function ($matches) use ($array) {
    return ( ( isset ( $array[$matches[1]] ) ) ? $array[$matches[1]] : '' );
} , $template );

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replace deprecated preg_replace /e with preg_replace_callback

From Dev

conditional preg_replace for optional backreference replacement

From Dev

Using preg_replace_callback

From Dev

preg_replace use matched pattern in replacement

From Dev

FROM preg_replace TO preg_replace_callback

From Dev

Using preg_replace on an array

From Dev

Replace preg_replace() to preg_replace_callback()

From Dev

Preg_replace_callback storing in array

From Dev

Laravel: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

From Dev

Get dynamic replacement with preg_replace()

From Dev

Laravel 5.1 - preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

From Dev

Using an array in preg_replace to reduce codes

From Dev

Laravel 4 Database insert Error - preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

From Dev

Preg_replace and Preg_replace_callback

From Dev

Codecolorer - preg_replace_callback()

From Dev

preg_replace with array index

From Dev

php converting preg_replace to preg_replace_callback

From Dev

laravel 5.2: ErrorException: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

From Dev

preg_replace_callback(): Requires argument 2, 'Array', to be a valid callback

From Dev

How to quote replacement in preg_replace?

From Dev

convert preg_replace to preg_replace_callback modifier error

From Dev

Allow write access to array in PHP preg_replace_callback

From Dev

preg_replace_callback regex issue, match with (.*?) returns array

From Dev

preg_replace to preg_replace_callback with an array as replacement

From Dev

Convert preg_replace to preg_replace_callback

From Dev

FROM preg_replace TO preg_replace_callback

From Dev

preg_replace_callback store match in global array and replace

From Dev

proper replacement for Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback?

From Dev

Get dynamic replacement with preg_replace()

Related Related

  1. 1

    Replace deprecated preg_replace /e with preg_replace_callback

  2. 2

    conditional preg_replace for optional backreference replacement

  3. 3

    Using preg_replace_callback

  4. 4

    preg_replace use matched pattern in replacement

  5. 5

    FROM preg_replace TO preg_replace_callback

  6. 6

    Using preg_replace on an array

  7. 7

    Replace preg_replace() to preg_replace_callback()

  8. 8

    Preg_replace_callback storing in array

  9. 9

    Laravel: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

  10. 10

    Get dynamic replacement with preg_replace()

  11. 11

    Laravel 5.1 - preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

  12. 12

    Using an array in preg_replace to reduce codes

  13. 13

    Laravel 4 Database insert Error - preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

  14. 14

    Preg_replace and Preg_replace_callback

  15. 15

    Codecolorer - preg_replace_callback()

  16. 16

    preg_replace with array index

  17. 17

    php converting preg_replace to preg_replace_callback

  18. 18

    laravel 5.2: ErrorException: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

  19. 19

    preg_replace_callback(): Requires argument 2, 'Array', to be a valid callback

  20. 20

    How to quote replacement in preg_replace?

  21. 21

    convert preg_replace to preg_replace_callback modifier error

  22. 22

    Allow write access to array in PHP preg_replace_callback

  23. 23

    preg_replace_callback regex issue, match with (.*?) returns array

  24. 24

    preg_replace to preg_replace_callback with an array as replacement

  25. 25

    Convert preg_replace to preg_replace_callback

  26. 26

    FROM preg_replace TO preg_replace_callback

  27. 27

    preg_replace_callback store match in global array and replace

  28. 28

    proper replacement for Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback?

  29. 29

    Get dynamic replacement with preg_replace()

HotTag

Archive