<?php
$string = 'I want to be transformed to a proper link: http://www.google.com But please leave me alone (<a href="https://www.google.com">https://www.google.com</a>).';
$regex = '~            # delimiter
              (?<![">])    # a neg. lookbehind
              https?://\S+ # http:// or https:// followed by not a whitespace
              \b           # a word boundary
              ~x';         # verbose to enable this explanation.
$string = preg_replace($regex, "<a href='$0'>$0</a>", $string);
echo $string;
?>