if (условие) $i = preg_replace('/https:\/\/site.com\/([^\s]*)a.jpg/U','https://site.com/$1b.jpg',$i);
print preg_replace('/https:\/\/site.com\/([^\s]*)a.jpg/U','https://site.com/$1b.jpg', 'https://site.com/testa.jpg');
// https://site.com/testb.jpg
print preg_replace('/https:\/\/site.com\/([^\s]*)a.jpg/U','https://site.com/$1b.jpg', 'https://site.com/testA.jpg');
// https://site.com/testA.jpg