• Resolved staze

    (@staze)


    Hi All,

    Realize this plugin is pretty much dead, but I do use it on a site, and was hoping someone had an easy fix for preg_replace being deprecated.

    In dirtycode.php, the block

    function trunc_c2c_preserve_code_formatting( $text ) {
        $text = str_replace(array('$', "'"), array('&#36&;', '&#39&;'), $text);
        $tags = array('code', 'pre');
        foreach ($tags as $tag) {
            $text = preg_replace(
                "^(<$tag>)\n?([\S|\s]*?)\n?(</$tag>)^i",
                "'<$tag>' . trunc_c2c_prep_code(\"$2\") . '</$tag>'",
                $text
            );
        }
        $text = str_replace(array('&#36&;', '&#39&;'), array('$', "'"), $text);
        return $text;
    }

    Needs to have the preg_replace replaced with a preg_replace_callback, but I’m having a hard time wrapping my head around it. Thoughts?

    • This topic was modified 7 years ago by staze.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Keith Driscoll

    (@keithdriscoll)

    Because your question is about coding, I would suggest asking your question here: https://www.ads-software.com/support/forum/wp-advanced/.

    Also take a look here and here.

    Good luck!

    Thread Starter staze

    (@staze)

    Sure, but it’s in relation to this plugin…

    I’ll dig in to those threads on SO.

    Thanks!

    Thread Starter staze

    (@staze)

    made it a little way with:

    $text = preg_replace_callback(
                "^(<$tag>)\n?([\S|\s]*?)\n?(</$tag>)^i",
                function ($m) { return '<$tag>' . trunc_c2c_prep_code($m[2]) . '</$tag>'; },
                $text
            );

    Problem is that it’s not replacing the <$tag> properly since they’re still a variable. =/

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Try function($m) use ($tag) { ... } instead. This should put the tag variable in scope of the function for you.

    Thread Starter staze

    (@staze)

    Thanks! Only issue is it still seems to echo a “<tag>” at the top of the output.

    I’ve now got:

    function trunc_c2c_preserve_code_formatting( $text ) {
        $text = str_replace(array('$', "'"), array('&#36&;', '&#39&;'), $text);
        $tags = array('code', 'pre');
        foreach ($tags as $tag) {
            $text = preg_replace_callback(
                "^(<$tag>)\n?([\S|\s]*?)\n?(</$tag>)^i",
                function ($m) use ($tag) { return '<$tag>' . trunc_c2c_prep_code($m[2]) . '</$tag>'; },
                $text
            );
        }
        $text = str_replace(array('&#36&;', '&#39&;'), array('$', "'"), $text);
        return $text;
    }
    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    '<$tag>' should be in double quotes in your function. Same for '</$tag>'. Single quotes won’t do the replacement for the variable.

    Thread Starter staze

    (@staze)

    That did it, thanks! =D

    Finished block:

    function trunc_c2c_preserve_code_formatting( $text ) {
        $text = str_replace(array('$', "'"), array('&#36&;', '&#39&;'), $text);
        $tags = array('code', 'pre');
        foreach ($tags as $tag) {
            $text = preg_replace_callback(
                "^(<$tag>)\n?([\S|\s]*?)\n?(</$tag>)^i",
                function ($m) use ($tag) { return "<$tag>" . trunc_c2c_prep_code($m[2]) . "</$tag>"; },
                $text
            );
        }
        $text = str_replace(array('&#36&;', '&#39&;'), array('$', "'"), $text);
        return $text;
    }
    Thread Starter staze

    (@staze)

    Closing! Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘PHP7 preg_replace broken’ is closed to new replies.