PHP preg urls2hyperlinks

2009/01/26 by Lassi A. Liikkanen

PHP preg_replace() urls2hyperlinks

This function will convert plain text URLs starting with http, https, or www into preformatted HTML links automatically. The latest update also automatically formats image links (identified by file extension into IMG tags:

function urls2Hyperlinks($string)
// modify first, add new line symbols if necessary 
', '
', '
'); $string = str_replace($changeThese,' \n
',$string); $string=nl2br($string); $in=array('`\s((?:https?|ftp)://\S+[[:alnum:]]/?)`si', '`\s((?$1 ', ' $1'); return preg_replace($in,$out,urls2Imgtags($string)); } function urls2Imgtags($string) { $in=array('`\s((?:https?|ftp)://\S+[[:alnum:]]/?).(png|jpg|jpeg|gif)`si' , '`\s((?
', ' IMG src='http://$1.$2' title='$1.$2' />
'); return preg_replace($in,$out,$string); }

This adaptation is based on user notes to PHP manual, preg_replace()

Related content:
How to convert MySQL enum type of field to keyed number field, 2008/03/16

No comments for this page:
Add comment | Show all comments

Your email will not be shown publicly or disclosed to third parties, it is used to validate messages
Location (City, Country):
* This information must be provided

Keywords: [php] , [www-techniques] Document's status: Ok (Document dates explained)

This document created: 2009/01/18
Modified: 2009/01/26
Published: 2009/01/26

This document's permanent URI (linking):

© Lassi A. Liikkanen 2009 - 2021. All rights reserved.

*Change layout:
Printable printable
Large text


@lassial Twitter feed: