I have a string which contains a user’s blog content. If they include the following:
www.google.com
http://google.com
http://www.google.com
in their blog post, I want PHP to replace these occurrences with a real hyperlink, but maintain the original substring (i.e. not change www.google.com to http://www.google.com).
Does anybody know how I might do this with PHP and regular expressions? I’ve tried this:
echo preg_replace('/((www|http:\/\/)[^ ]+)/', '<a href="$1" target="_blank">$1</a>', $content);
But this only succeeds if you end the link with a space. Fails if you end it with a comma or full stop.
1 Answer