简体   繁体   中英

How do I convert the following to preg_replace_callback (for use in PHP 7.3.2)

Trying to get the code below to not use the /e modifier (using preg_replace_callback ) instead.

private function encodeHeader($input, $charset = 'ISO-8859-1')
{
    preg_match_all('/(\w*[\x80-\xFF]+\w*)/', $input, $matches);
    foreach ($matches[1] as $value) {
        $replacement = preg_replace('/([\x80-\xFF])/e', '"=" . strtoupper(dechex(ord("\1")))', $value);
        $input = str_replace($value, '=?' . $charset . '?Q?' . $replacement . '?=', $input);
    }

    return $input;
}

Just use an anonymous function to return what you had, using the $m parameter as the matches, so $m[1] instead of \\1 :

$replacement = preg_replace_callback('/([\x80-\xFF])/',
                                     function($m) {
                                         return "=" . strtoupper(dechex(ord($m[1])));
                                     }, $value);

If you just use '/[\\x80-\\xFF]/' with no capture group, then you use $m[0] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM