This is quite straightforward with user_roles() and array_search(). Below is a function which will return the role ID if there is a role matching the name and FALSE otherwise.
function get_role_by_name($name) {
$roles = user_roles();
return array_search($name, $roles);
}
// 例子 根据角色名得到角色id
$rid = get_role_by_name('administrator');
One liner would be:
$rid = array_search('administrator', user_roles);
来自 http://drupal.stackexchange.com/questions/50435/how-do-i-find-the-role-id-from-role-name