using preg_match to find all values in brackets and pass them to variable

Instead of using preg_match, you can use preg_replace_callback and do the replacement on the input $str for every match yielded by $aMatches.

The value of capture group 1 is in $aMatches[1]

For example:

function gdrcd_chatcolor3($str) {
    return preg_replace_callback('#\[([^][]*)\]#', function($aMatches) {
        $caricaoggetto = gdrcd_query("SELECT * FROM oggetto WHERE oggetto.nome='".gdrcd_filter('get', $aMatches[1])."'");
        $urloggetto= "javascript:modalWindow('oggetto', 'Oggetto', 'popup.php?page=popupitem&oggetto=". $caricaoggetto['id_oggetto'] ."', 800, 450);";
        return '<a href="' . $urloggetto . '"><span style="color: #c21616;">'.$aMatches[1].'</span></a>';
    }, $str);
}

Note that you don't have to use intermediate strings and variables like

$fullString = $chat_message;
$sTest = $str;