Requires: Preprocess
<?php
/**
* class Wordbreak
* 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 0.2
*/
require_once 'Preprocess.php';
class Wordbreak extends Preprocess {
private
$len,
$char;
public function __construct ($len = 76, $char = ' ') {
$this->len = $len;
$this->char = $char;
}
public function run ($text) {
$this->text =& $text;
$this->_escape_init();
$this->_escape_comments_cdata();
$this->_escape_content ('head', 'title', 'textarea');
$text = '>'.$text;
$text = preg_replace_callback ('~(?<=>)[^<]*~', array ($this, '_find'), $text);
$text = substr ($text, 1);
$this->_escape_fin();
return $text;
}
private function _find ($match) {
return preg_replace_callback ('~\S{' . $this->len . ',}~', array ($this, '_wrap'), $match[0]);
}
private function _wrap ($match) {
return substr (chunk_split ($match[0], $this->len, $this->char), 0, - strlen ($this->char));
}
}
?>
<?php
ob_start (array (new Wordbreak (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>wrap test</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:
<a href="http://zeus.zeit.de/text/archiv/1999/50/199950.lutwak-interview.xml">
http://zeus.zeit.de/text/archiv/1999/50/199950.lutwak-interview.xml
</a>
</p>
</body>
</html>