'Fatal error: Array and string offset access syntax with curly braces is no longer supported in php 8.0 pkcs5_unpad

I got this error after copying code from a tutorial. It's deprecated and I tried resolving it:

Array and string offset access syntax with curly braces is no longer supported in .../app/Http/Helpers/helpers.php

My code copy:

if (!function_exists("pkcs5_unpad_e")) {
    function pkcs5_unpad_e($text) {
        $pad = ord($text{strlen($text) - 1});
        if ($pad > strlen($text))
            return false;

        return substr($text, 0, -1 * $pad);
    }
}

My edit: replace {....} with [.....] ==> HTTP ERROR 500

if (!function_exists("pkcs5_unpad_e")) {
    function pkcs5_unpad_e($text) {
        $pad = ord($text[strlen($text) - 1]);
        if ($pad > strlen($text))
                return false;
            
        return substr($text, 0, -1 * $pad);
    }
}

I am using Ubuntu 20.04 DigitalOcean Cyberpanel server and PHP 8.



Solution 1:[1]

Simply replace {} with []

PS. No idea why nobody creates an answer instead of a comment. So please mark this question solved.

PPS. I mark this answer community, because I don't want points for a solution provided by someone else.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1