src/KsUtils/Str.php
public static function upperToUnderscores($str)
    {
        $ret = array();
        $len = strlen($str);
        $uppChars = "ABCDEFGHIJKLMNOQPRSTUVWXYZ";

        for ($i = 0; $i < $len; $i++) {
            $chr = $str[$i];
            $ret[] = strstr($uppChars, $chr) !== false
                    ? "_" . strtolower($chr)
                    : $chr;
        }

        return implode("", $ret);
    }
public static function upperToUnderscores($str)
    {
        $ret = array();
        $len = strlen($str);
        $uppChars = "ABCDEFGHIJKLMNOQPRSTUVWXYZ";

        for ($i = 0; $i < $len; $i++) {
            $chr = $str[$i];
            $ret[] = strstr($uppChars, $chr) !== false
                    ? "_" . strtolower($chr)
                    : $chr;
        }

        return implode("", $ret);
    }
Replaces all uppercase chars to lowercase char with preceeding underscore (userIdKey -> user_id_key)
@param  string $str Source string
@return string Destination string