Benötigt: Preprocess
<?php
/**
* class Linkify
* Copyright (C) 2006 Dao Gottwald <dao at design-noir.de>
*
* Licensed under the terms of the GNU Lesser General Public License:
* http://www.opensource.org/licenses/lgpl-license.php
*
* @version 1.1.1
*/
require_once 'Preprocess.php';
class Linkify extends Preprocess {
private
$display_length;
public function __construct ($display_length = 40) {
$this->display_length = $display_length;
}
public function run ($text) {
$this->text =& $text;
$this->_escape_init();
$this->_escape_comments_cdata();
$this->_escape_content ('head', 'pre', 'code', 'blockcode', 'textarea', 'a');
$text = preg_replace_callback ('~>(.*?)<~s', array ($this, '_findURL'), '>'. $text .'<');
$text = substr ($text, 1, -1);
$this->_escape_fin();
return $text;
}
private function _findURL ($match) {
return '>'. preg_replace_callback ('§(?<=\b)
(?:
(?:https?|ftp)://
|
www\.
)
[\w\.]+
[;#&/~=\w+\-()?\.,:%]*
[;#&/~=\w+\-(]§ix', array ($this, '_linkURL'), $match[1]) .'<';
}
private function _linkURL ($url) {
$name = html_entity_decode ($url[0]);
$name = preg_replace ('~^http://www\.~', 'www.', $name);
if (strlen ($name) > $this->display_length) {
$name = substr ($name, 0, $this->display_length - 15).'…'.substr ($name, -12);
}
$url[0] = preg_replace ('~^www\.~', 'http://www.', $url[0]);
return '<a href="'.$url[0].'">'.$name.'</a>';
}
}
?>
<?php
ob_start (array (new Linkify (35), 'run'));
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
<head>
<title>Automatische Verlinkung</title>
<style type="text/css"><!--
p {
border: 1px solid red;
width: 13em;
}
--></style>
</head>
<body>
<p>
Der US-Ökonom Edward Luttwak über den globalen Vormarsch des Turbokapitalismus
- und über Gerhard Schröder und Tony Blair.
Ein ZEIT-Gespräch: http://zeus.zeit.de/text/archiv/1999/50/199950.lutwak-interview.xml
</p>
</body>
</html>