Title: PHP7 preg_replace broken
Last modified: February 19, 2018

---

# PHP7 preg_replace broken

 *  Resolved [staze](https://wordpress.org/support/users/staze/)
 * (@staze)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/php7-preg_replace-broken/)
 * 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 8 years, 3 months ago by [staze](https://wordpress.org/support/users/staze/).

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [Keith](https://wordpress.org/support/users/keithdriscoll/)
 * (@keithdriscoll)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/php7-preg_replace-broken/#post-9991716)
 * Because your question is about coding, I would suggest asking your question here:
   [https://wordpress.org/support/forum/wp-advanced/](https://wordpress.org/support/forum/wp-advanced/).
 * Also take a look [here](https://stackoverflow.com/questions/15454220/replace-preg-replace-e-modifier-with-preg-replace-callback)
   and [here](https://stackoverflow.com/questions/19245205/replace-deprecated-preg-replace-e-with-preg-replace-callback).
 * Good luck!
 *  Thread Starter [staze](https://wordpress.org/support/users/staze/)
 * (@staze)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/php7-preg_replace-broken/#post-9991746)
 * Sure, but it’s in relation to this plugin…
 * I’ll dig in to those threads on SO.
 * Thanks!
 *  Thread Starter [staze](https://wordpress.org/support/users/staze/)
 * (@staze)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/php7-preg_replace-broken/#post-9991842)
 * 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)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [8 years, 3 months ago](https://wordpress.org/support/topic/php7-preg_replace-broken/#post-9991925)
 * Try `function($m) use ($tag) { ... }` instead. This should put the tag variable
   in scope of the function for you.
 *  Thread Starter [staze](https://wordpress.org/support/users/staze/)
 * (@staze)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/php7-preg_replace-broken/#post-9991979)
 * 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)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [8 years, 3 months ago](https://wordpress.org/support/topic/php7-preg_replace-broken/#post-9992102)
 * `'<$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](https://wordpress.org/support/users/staze/)
 * (@staze)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/php7-preg_replace-broken/#post-9992109)
 * 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](https://wordpress.org/support/users/staze/)
 * (@staze)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/php7-preg_replace-broken/#post-9992110)
 * Closing! Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘PHP7 preg_replace broken’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/dirty-code.svg)
 * [Dirty Code](https://wordpress.org/plugins/dirty-code/)
 * [Support Threads](https://wordpress.org/support/plugin/dirty-code/)
 * [Active Topics](https://wordpress.org/support/plugin/dirty-code/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/dirty-code/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/dirty-code/reviews/)

## Tags

 * [PHP7](https://wordpress.org/support/topic-tag/php7/)

 * 8 replies
 * 3 participants
 * Last reply from: [staze](https://wordpress.org/support/users/staze/)
 * Last activity: [8 years, 3 months ago](https://wordpress.org/support/topic/php7-preg_replace-broken/#post-9992110)
 * Status: resolved