$re = '/^((?:\w[\w-]{0,61}\.)*)(\w[\w-]{0,61}\.[a-z]{2,62})$/mi';
$str = 'sites.example.com
m.example.com
www.example.com
ww2.example.com
mail.example.com
blog.example.com
shop.example.com
cdn.example.com
api.example.com
1.example.com
example.com
section.sub-domain.example.com';
$subst = "$2";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php