• Resolved brbrbr

    (@brbrbr)


    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)

    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

    (@shazahm1hotmailcom)

    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:

    Thread Starter brbrbr

    (@brbrbr)

    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

    (@shazahm1hotmailcom)

    Darn, yes, the function_exists() check does need the namespace too.

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

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