protected function escapeManualWithFlags($value, $ignore = '', $flags = 0)
    {
        // Convert ignore string into an array
        $ignores = str_split($ignore);

        $escapeFilter = ['\\', '*', '(', ')'];
        $escapeDn = ['\\', ',', '=', '+', '<', '>', ';', '"', '#'];

        switch ($flags) {
            case 1:
                // Int 1 equals to LDAP_ESCAPE_FILTER
                $escapes = $escapeFilter;
                break;
            case 2:
                // Int 2 equals to LDAP_ESCAPE_DN
                $escapes = $escapeDn;
                break;
            case 3:
                // If both LDAP_ESCAPE_FILTER and LDAP_ESCAPE_DN are used
                $escapes = array_merge($escapeFilter, $escapeDn);
                break;
            default:
                return false;
        }

        foreach ($escapes as $escape) {
            // Make sure the escaped value isn't being ignored
            if (!in_array($escape, $ignores)) {
                $hexed = chunk_split(bin2hex($escape), 2, '\\');

                $hexed = '\\'.substr($hexed, 0, -1);

                $value = str_replace($escape, $hexed, $value);
            }
        }

        return $value;
    }