fork download
  1. <?php // http://stackoverflow.com/q/33180712/5290909
  2. // Replace URLs inside <pre> tags
  3.  
  4. $postbits = "
  5. <pre>
  6. http://www.google.com
  7.  
  8. http://w...content-available-to-author-only...o.com
  9.  
  10. http://www.microsoft.com/ <-- only this one clickable
  11. </pre>";
  12.  
  13.  
  14. $regex = '#\G((?:(?!\A)|.*<pre)(?:(?!</pre>).)*)(https?://\S+?)#isU';
  15. $repl = '\1<a href="\2" target="_blank">\2</a>';
  16.  
  17. $postbits = preg_replace( $regex, $repl, $postbits);
  18.  
  19. echo $postbits;
  20.  
  21. ?>
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
<pre>
<a href="http://www.google.com" target="_blank">http://www.google.com</a>

<a href="http://w...content-available-to-author-only...o.com" target="_blank">http://w...content-available-to-author-only...o.com</a>

<a href="http://www.microsoft.com/" target="_blank">http://www.microsoft.com/</a> <-- only this one clickable
</pre>