8.12. Wie mache ich aus URIs im Text anklickbare Links?Antwort von Björn SchotteBesten Dank an Thomas Weinert, von dem die ursprüngliche RegExp stammt. Folgender regulärer Ausdruck ersetzt alle normalen URIs, das heißt zum Beispiel http://www.phpcenter.de/, news:de.comp.lang.php, mailto:bjoern@thinkphp.de oder ftp://ftp.suse.com/ durch HTML-Code, damit diese URIs für den Benutzer klickbar werden.
/**
* replace URIs with appropriate HTML code to be clickable.
*/
function replace_uri($str) {
$pattern = '#(^|[^\"=]{1})(http://|ftp://|mailto:|news:)([^\s<>]+)([\s\n<>]|$)#sm';
return preg_replace($pattern,"\\1<a href=\"\\2\\3\"><u>\\2\\3</u></a>\\4",$str);
}
|
||
| 8.12. Wie mache ich aus URIs im Text anklickbare Links? http://www.php-faq.de/q/q-regexp-uri-klickbar.html |
||
| de.comp.lang.php.* FAQ | (c) Copyright 2000-2007 Das dclp-FAQ-Team | ||