Allow write access to array in PHP preg_replace_callback

praine

I'm trying to write to an array which was initialized outside an anonymous preg_replace_callback function. I've tried the "use" keyword, and also declaring the variable "global", but neither seem to work.

Here is my code:

$headwords=array_keys($dict);
$replaced=array();
// Scan text

foreach($headwords as $index=>$headword){
    if(preg_match("/\b".$headword."\b/", $transcript)){
        $transcript=preg_replace_callback("/(\b".$headword."\b)/", function($m) use($index, $replaced){
            $replaced[$index]=$m[1];
            return "<".$index.">";
        }, $transcript);
    }
}

A var_dump of "$replaced" shows it as an empty array. The preg_replace_callback loop is part of a public class function, if that makes any difference.

I've tried Googling this issue but with no success. Very grateful for any help with this problem.

Yehia Awad

You're almost there: you forget to add the reference & to $replaced variable inside use parameter if you are planning to reuse it outside the scope.

   $transcript=preg_replace_callback("/(\b".$headword."\b)/", function($m) use($index, &$replaced){
        $replaced[$index]=$m[1];
        return "<".$index.">";
    }, $transcript);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Preg_replace_callback storing in array

From Dev

PHP preg_replace_callback ignore id

From Dev

preg_replace_callback not working in php

From Dev

Php, Regex preg_replace_callback

From Dev

Php, Regex preg_replace_callback

From Dev

PHP REGEX preg_replace_callback not matching

From Dev

preg_replace to preg_replace_callback with an array as replacement

From Dev

preg_replace to preg_replace_callback with an array as replacement

From Dev

php converting preg_replace to preg_replace_callback

From Dev

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

From Dev

preg_replace_callback store match in global array and replace

From Dev

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

From Dev

PHP add new line \n into preg_replace_callback function

From Dev

pcre.trackback limit php preg_replace_callback()

From Dev

php regex preg_replace_callback fails to handle mutliple lines

From Dev

PHP add new line \n into preg_replace_callback function

From Dev

php: preg_replace_callback remove detected content with its patterns

From Dev

Securely Allow PHP Read & Write Access to System Files

From Dev

Parse sentence with php preg_replace_callback and ignore data inside parenthesis

From Dev

Using php preg_replace_callback - but replacing whole string not each sub string

From Dev

Get Add block after 1st image using preg_replace_callback PHP

From Dev

Parse sentence with php preg_replace_callback and ignore data inside parenthesis

From Dev

allow php to write log files

From Dev

Allow write access for regular user on CIFS share

From Dev

Allow access to particular file in PHP

From Dev

PHP Write Array To Csv

From Dev

PHP Write Array To Csv

From Dev

Using preg_replace_callback

From Dev

Codecolorer - preg_replace_callback()

Related Related

  1. 1

    Preg_replace_callback storing in array

  2. 2

    PHP preg_replace_callback ignore id

  3. 3

    preg_replace_callback not working in php

  4. 4

    Php, Regex preg_replace_callback

  5. 5

    Php, Regex preg_replace_callback

  6. 6

    PHP REGEX preg_replace_callback not matching

  7. 7

    preg_replace to preg_replace_callback with an array as replacement

  8. 8

    preg_replace to preg_replace_callback with an array as replacement

  9. 9

    php converting preg_replace to preg_replace_callback

  10. 10

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

  11. 11

    preg_replace_callback store match in global array and replace

  12. 12

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

  13. 13

    PHP add new line \n into preg_replace_callback function

  14. 14

    pcre.trackback limit php preg_replace_callback()

  15. 15

    php regex preg_replace_callback fails to handle mutliple lines

  16. 16

    PHP add new line \n into preg_replace_callback function

  17. 17

    php: preg_replace_callback remove detected content with its patterns

  18. 18

    Securely Allow PHP Read & Write Access to System Files

  19. 19

    Parse sentence with php preg_replace_callback and ignore data inside parenthesis

  20. 20

    Using php preg_replace_callback - but replacing whole string not each sub string

  21. 21

    Get Add block after 1st image using preg_replace_callback PHP

  22. 22

    Parse sentence with php preg_replace_callback and ignore data inside parenthesis

  23. 23

    allow php to write log files

  24. 24

    Allow write access for regular user on CIFS share

  25. 25

    Allow access to particular file in PHP

  26. 26

    PHP Write Array To Csv

  27. 27

    PHP Write Array To Csv

  28. 28

    Using preg_replace_callback

  29. 29

    Codecolorer - preg_replace_callback()

HotTag

Archive