Katsu
Katsu

Reputation: 41

preg_replace_callback(): Requires argument 2 to be a valid callback

I'm learning PHP with some pre-made websites so I can see how it's done because I'm a visual learner, but I've run into a problem.

I got an error saying

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

Now I know this happens because of a newer PHP that doesn't support preg_replace() but preg_replace_callback(), but upon changing it, I get this error:

preg_replace_callback(): Requires argument 2, 'preg_replace('/(.{59})/', '\$1< >', '$1')', to be a valid callback

The code makes too long messages in a chatbox divided into two separate ones to keep all letters inside the chat window.

The error was located in line 20, where only the $msg); stands.

if ($shoutbox['fixLongWords'] > 5 && $shoutbox['fixLongWords'] < $shoutbox['maxMsgLenght'])
{
    $non_breaking_space = $context['utf8'] ? ($context['server']['complex_preg_chars'] ? '\x{A0}' : "\xC2\xA0") : '\xA0';

    if ($context['browser']['is_gecko'] || $context['browser']['is_konqueror'])
        $breaker = '<span style="margin:0 -0.5ex 0 0"> </span>';
    elseif ($context['browser']['is_opera'])
        $breaker = '<span style="margin:0 -0.65ex 0 -1px"> </span>';
    else
        $breaker = '<span style="width:0;margin:0 -0.6ex 0 -1px"> </span>';

    $shoutbox['fixLongWords'] = (int) min(1024, $shoutbox['fixLongWords']);

    if (strlen($msg) > $shoutbox['fixLongWords'])
    {
        $msg = strtr($msg, array($breaker => '< >', '&nbsp;' => $context['utf8'] ? "\xC2\xA0" : "\xA0"));
        $msg = preg_replace(
            '~(?<=[>;:!? ' . $non_breaking_space . '\]()]|^)([\w\.]{' . $shoutbox['fixLongWords'] . ',})~e' . ($context['utf8'] ? 'u' : ''),
            'preg_replace(\'/(.{' . ($shoutbox['fixLongWords'] - 1) . '})/' . ($context['utf8'] ? 'u' : '') . '\', \'\\$1< >\', \'$1\')',
            $msg);
        $msg = strtr($msg, array('< >' => $breaker, $context['utf8'] ? "\xC2\xA0" : "\xA0" => '&nbsp;'));
    }
}

Anyone have any idea what might be causing the problem?

Upvotes: 0

Views: 2864

Answers (1)

M1ke
M1ke

Reputation: 6406

The PHP documentation describes this function as

Perform a regular expression search and replace using a callback:

mixed preg_replace_callback ( mixed $pattern , callable $callback , mixed $subject [, int $limit = -1 [, int &$count ]] )

The callable class requires the name of a function (as a string, e.g. 'print_r') or an actual function function(){ // do something }.

The preg_replace_callback() process will pass an array of matches into the "callable function" and return a string. From the docs, describing the second parameter:

A callback that will be called and passed an array of matched elements in the subject string. The callback should return the replacement string. This is the callback signature:

string handler ( array $matches )

Upvotes: 2

Related Questions