function to_standard_format($url) {
$pattern = '#(https?://)?\w+\.example.com(?=[/\s]|$)#i';
return preg_replace($pattern, 'http://www.example.com', $url);
}
var_dump(to_standard_format('http://www.example.com'));
var_dump(to_standard_format('https://in.example.com'));
var_dump(to_standard_format('http://uk.example.com'));
var_dump(to_standard_format('http://uk.example.com/blah'));
var_dump(to_standard_format('uk.example.com'));
var_dump(to_standard_format('http://www.example.com.au'));
var_dump(to_standard_format('uk.another-example.com'));
印刷
string(22) "http://www.example.com"
string(22) "http://www.example.com"
string(22) "http://www.example.com"
string(27) "http://www.example.com/blah"
string(22) "http://www.example.com"
string(25) "http://www.example.com.au"
string(22) "uk.another-example.com"