使用php中的正则表达式简化URL

I have a URL like this

https://www.domain.com/course/undergraduate/details/name/subject-name/alpha/a/search-all/

Expected URL : https://www.domain.com/course/undergraduate/subject-name

To get the Expected URL i did like this

$parse=parse_url($url);


$scheme=$parse['scheme'];
$host=$parse['host'];
$path=$parse['path'];

$posted = substr($path, 0, strpos($path, "/alpha/"));


$segments = explode('/', $posted);
$numSegments = count($segments); 

unset($segments['3']);
unset($segments['4']);

$new_url=implode("/",$segments);

echo $scheme."://".$host.$new_url;

Here i dont want to follow these process. I need to simplify this process to get the expected url. Could any one suggest how to achieve with regular expression