fork download
  1. <?php
  2. $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>).';
  3. $regex = '~ # delimiter
  4. (?<![">]) # a neg. lookbehind
  5. https?://\S+ # http:// or https:// followed by not a whitespace
  6. \b # a word boundary
  7. ~x'; # verbose to enable this explanation.
  8. $string = preg_replace($regex, "<a href='$0'>$0</a>", $string);
  9. echo $string;
  10. ?>
Success #stdin #stdout 0.02s 52480KB
stdin
Standard input is empty
stdout
I want to be transformed to a proper link: <a href='http://www.google.com'>http://www.google.com</a> But please leave me alone (<a href="https://www.google.com">https://www.google.com</a>).