I have the url inputs in various formats. I want to convert all into one standard format. Few example inputs are below.
From above all the inputs, I wish to convert into first one (http://www.example.com) with www. So I think we can replace the string if "www" not there.
$pattern = '/((http?|https)\:\/\/)?([a-zA-Z]+)\.example.com\/[a-z]{2}\/[a-zA-Z0-9]{5,30}/i';
I tried this pattern. I know it will match replace everything. SO please suggest me, how to do that.
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'));
prints
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"
as far as i understand, you just need to replace first part with "" (empty string). So you need pattern like https?://(www.)?
i.e. preg_replace("https?://(www.)?", "http://www.example.com", "")