Title: mb_substr_replace kills php
Last modified: March 27, 2021

---

# mb_substr_replace kills php

 *  Resolved [brbrbr](https://wordpress.org/support/users/brbrbr/)
 * (@brbrbr)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/mb_substr_replace-kills-php/)
 * Your approach to implement mb_substr_replace is consuming quite some memory and
   on some of my pages it reaches the memory limit. Killing the whole lot.
    it’s
   in the
 *     ```
       preg_match_all( '/./us', (string) $string, $smatches );
       preg_match_all( '/./us', (string) $replacement, $rmatches );
       ```
   
 * I found a different approach using mb_substr. ([https://pastebin.com/YBxpMXMv](https://pastebin.com/YBxpMXMv))
 * would it be possible to wrap your mb_substr_replace in a function_exists? So 
   I can override the function without editing your plugin files?

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

 *  [Steven](https://wordpress.org/support/users/shazahm1hotmailcom/)
 * (@shazahm1hotmailcom)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/mb_substr_replace-kills-php/#post-14249372)
 * **RE: would it be possible to wrap your mb_substr_replace in a function_exists?
   So I can override the function without editing your plugin files?**
 * Add and will be in the next release. FYI, the are already functions are namespace
   and are not global.
 * Link showing commit of changes:
    - [https://github.com/shazahm1/Easy-Table-of-Contents/commit/7945ab282c7b80dd9f23e3f49ab90c7b8d337a57](https://github.com/shazahm1/Easy-Table-of-Contents/commit/7945ab282c7b80dd9f23e3f49ab90c7b8d337a57)
 *  Thread Starter [brbrbr](https://wordpress.org/support/users/brbrbr/)
 * (@brbrbr)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/mb_substr_replace-kills-php/#post-14249724)
 * Hi,
    thanks,
 * I overlooked the namespace. I applied your patch manually. However I had to change
   it.
    (I’m not very familiar with namespaces)
 * `if ( ! function_exists( 'Easy_Plugins\Table_Of_Contents\String\mb_substr_replace')):`
 * I created a file mu-plugins/mb_substr_replace.php and now it works excellent.
 *     ```
       <?php
   
       namespace Easy_Plugins\Table_Of_Contents\String;
   
       function mb_substr_replace( $string, $replacement, $start, $length = null )
       {
           if (is_array($string) ) {
                   $num = count($string);
                   $replacement = is_array($replacement) ? array_slice($replacement, 0, $num) : array_pad(array( $replacement ), $num, $replacement);
   
                   // $start
               if (is_array($start) ) {
                       $start = array_slice($start, 0, $num);
                   foreach ( $start as $key => $value ) {
                       $start[ $key ] = is_int($value) ? $value : 0;
                   }
               } else {
                       $start = array_pad(array( $start ), $num, $start);
               }
   
                   // $length
               if (! isset($length) ) {
                       $length = array_fill(0, $num, 0);
               } elseif (is_array($length) ) {
                       $length = array_slice($length, 0, $num);
                   foreach ( $length as $key => $value ) {
                           $length[ $key ] = isset($value) ? ( is_int($value) ? $value : $num ) : 0;
                   }
               } else {
                       $length = array_pad(array( $length ), $num, $length);
               }
   
                   // Recursive call
                   return array_map(__FUNCTION__, $string, $replacement, $start, $length);
           }
               $string_length =  mb_strlen($string);
           if ($start < 0) { $start = max(0, $string_length + $start);
           }
           else if ($start > $string_length) {$start = $string_length;
           }
           if ($length < 0) { $length = max(0, $string_length - $start + $length);
           }
           else if ((is_null($length) === true) || ($length > $string_length)) { $length = $string_length;
           }
           if (($start + $length) > $string_length) {$length = $string_length - $start;
           }
               $string= mb_substr($string, 0, $start) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length);
               return $string;
       }
       ```
   
 *  [Steven](https://wordpress.org/support/users/shazahm1hotmailcom/)
 * (@shazahm1hotmailcom)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/mb_substr_replace-kills-php/#post-14249774)
 * Darn, yes, the `function_exists()` check does need the namespace too.
    - [https://github.com/shazahm1/Easy-Table-of-Contents/commit/b8f5f1423019505669d68f0527cc77755b961a53](https://github.com/shazahm1/Easy-Table-of-Contents/commit/b8f5f1423019505669d68f0527cc77755b961a53)

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

The topic ‘mb_substr_replace kills php’ is closed to new replies.

 * ![](https://ps.w.org/easy-table-of-contents/assets/icon-256x256.png?rev=3045459)
 * [Easy Table of Contents](https://wordpress.org/plugins/easy-table-of-contents/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/easy-table-of-contents/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/easy-table-of-contents/)
 * [Active Topics](https://wordpress.org/support/plugin/easy-table-of-contents/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/easy-table-of-contents/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/easy-table-of-contents/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Steven](https://wordpress.org/support/users/shazahm1hotmailcom/)
 * Last activity: [5 years, 2 months ago](https://wordpress.org/support/topic/mb_substr_replace-kills-php/#post-14249774)
 * Status: resolved