Here we can use a preg_replace
, with a simple expression, maybe similar to:
(.+\.com\/.+?\/).+
where we are capturing our desired output using this capturing group:
(.+\.com\/.+?\/)
and then we swipe to the end of string and replace it with $1
.
Test
$re = '/(.+\.com\/.+?\/).+/m';
$str = 'http://www.example.com/part1/part2/part3/part4';
$subst = '$1';
$result = preg_replace($re, $subst, $str);
echo $result;
RegEx Circuit
jex.im visualizes regular expressions:

For all domains .com
or not, we might be able to solve it with this expression:
(.+\..+?\/.+?\/).+
Test
$re = '/(.+\..+?\/.+?\/).+/m';
$str = 'http://www.example.com/part1/part2/part3/part4';
$subst = '$1';
$result = preg_replace($re, $subst, $str);
echo $result;