Preg_replace and Preg_replace_callback

Ralf

I'm updating my coding and moving away from preg_replace towards the function preg_replace_callback. On the first I used two arrays, which replaced all the matches in a piece of text.

I have setup the following new piece of code, but I am running into some issues:

<?php
$inhoud = " dit is een test versie, waarin [alum] staat [alum] & [fotoalbums] om [intern=test]te[/intern] vervangen<p>";

function parse_callback($match) {
        $hit = $match[0];
        switch ($hit){
                case '[alum]': 
                        return "<a href=\"/linktype1/\">link1</a>";
                        break;
                case '[fotoalbums]':
                        return "<a href=\"/linktype2/\">link2</a>";
                        break;
                case '[intern]':
                        return "<a href=\"". $match[1] ."\">$match[2]</a>";
                        break;
                default:
                        //return "UNKNOWN:$match";
                        return var_dump($match);
        }
}

$Patroon = "'\[intern=(.*?)\](.*?)\\[\/intern\]'";
$Patroon = "'\[fotoalbums\]'";
$Patroon = "'\[alum\]'";

$inhoud = preg_replace_callback($Patroon, parse_callback, $inhoud);
?>

The later two in the $Patroon are no issue, these will be updated, but I'm unable to update the first, as the match being found will be the complete string. The $match will also contain the pieces of text from $inhoud on which is has matched (.*?), but I'm unable to process these in the switch.

Any suggestions on how to resolve this issue or a better approach to the coding. The above example is just a few options which will be matched, the actual list is much larger.

Mariano

First of all, you should pass the function name as a string: "parse_callback".

As you've noticed, match[0] will return the whole matched text ("[intern=test]te[/intern]"). You could capture the tag name in a group:

$Patroon = "'\[(intern)=(.*?)\](.*?)\[/intern\]'";
  • Notice the parentheses in (intern)

This way, the tag name will be returned by $match[1].

And using this approach to all patterns:

$Patroon = array(
    "'\[(intern)=(.*?)\](.*?)\[/intern\]'",
    "'\[(fotoalbums|alum)\]'"
);

Code

<?php
$inhoud = " dit is een test versie, waarin [alum] staat [alum] & [fotoalbums] om [intern=test]te[/intern] vervangen<p>";

function parse_callback($match) {
        //check the value of the first capture
        $hit = $match[1];
        switch ($hit){
                case 'alum': 
                        return "<a href=\"/linktype1/\">link1</a>";
                        break;
                case 'fotoalbums':
                        return "<a href=\"/linktype2/\">link2</a>";
                        break;
                case 'intern':
                        return "<a href=\"$match[2]\">$match[3]</a>";
                        break;
                default:
                        //return "UNKNOWN:$match";
                        return var_dump($match);
        }
}

$Patroon = array(
    "'\[(intern)=(.*?)\](.*?)\[/intern\]'",
    "'\[(fotoalbums|alum)\]'"
);

$inhoud = preg_replace_callback($Patroon, "parse_callback", $inhoud);

echo $inhoud;
?>

Output

 dit is een test versie, waarin <a href="/linktype1/">link1</a> staat <a href="/linktype1/">link1</a> & <a href="/linktype2/">link2</a> om <a href="test">te</a> vervangen<p>

ideone demo

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

Replace preg_replace() to preg_replace_callback()

From Dev

preg_replace to preg_replace_callback with an array as replacement

From Dev

convert preg_replace to preg_replace_callback modifier error

From Dev

FROM preg_replace TO preg_replace_callback

From Dev

php converting preg_replace to preg_replace_callback

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

Using preg_replace_callback

From Dev

Codecolorer - preg_replace_callback()

From Dev

Replace 'e' modifier with preg_replace_callback

From Dev

Replacing preg_replace by preg_replace_callback is giving me a warning

From Dev

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in

From Dev

Replacing preg_replace (with deprecated /e parameter) to preg_replace_callback

From Dev

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

From Dev

mpdf error - preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

From Dev

Can't convert preg_replace() with modifier /e to preg_replace_callback()

From Dev

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

From Dev

Replacing preg_replace by preg_replace_callback is giving me a warning

From Dev

WP-lightbox Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

From Dev

PHP preg_replace_callback ignore id

From Dev

preg_replace_callback: Unknown modifier '!'

From Dev

Replacing /e modifier with preg_replace_callback

From Dev

preg_replace_callback not working in php

From Dev

preg_replace_callback returns duplicate values

From Dev

Php, Regex preg_replace_callback

From Dev

Using preg_replace_callback with shortcodes

From Dev

sort properties with preg_replace_callback

Related Related

  1. 1

    Replace deprecated preg_replace /e with preg_replace_callback

  2. 2

    Replace preg_replace() to preg_replace_callback()

  3. 3

    preg_replace to preg_replace_callback with an array as replacement

  4. 4

    convert preg_replace to preg_replace_callback modifier error

  5. 5

    FROM preg_replace TO preg_replace_callback

  6. 6

    php converting preg_replace to preg_replace_callback

  7. 7

    preg_replace to preg_replace_callback with an array as replacement

  8. 8

    Convert preg_replace to preg_replace_callback

  9. 9

    FROM preg_replace TO preg_replace_callback

  10. 10

    Using preg_replace_callback

  11. 11

    Codecolorer - preg_replace_callback()

  12. 12

    Replace 'e' modifier with preg_replace_callback

  13. 13

    Replacing preg_replace by preg_replace_callback is giving me a warning

  14. 14

    Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in

  15. 15

    Replacing preg_replace (with deprecated /e parameter) to preg_replace_callback

  16. 16

    Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

  17. 17

    mpdf error - preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

  18. 18

    Can't convert preg_replace() with modifier /e to preg_replace_callback()

  19. 19

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

  20. 20

    Replacing preg_replace by preg_replace_callback is giving me a warning

  21. 21

    WP-lightbox Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

  22. 22

    PHP preg_replace_callback ignore id

  23. 23

    preg_replace_callback: Unknown modifier '!'

  24. 24

    Replacing /e modifier with preg_replace_callback

  25. 25

    preg_replace_callback not working in php

  26. 26

    preg_replace_callback returns duplicate values

  27. 27

    Php, Regex preg_replace_callback

  28. 28

    Using preg_replace_callback with shortcodes

  29. 29

    sort properties with preg_replace_callback

HotTag

Archive