Requires: Preprocess
<?php
/**
* class Entitify
* Copyright (C) 2009 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.0.2
*/
require_once 'Preprocess.php';
class Entitify extends Preprocess {
private
$entities_from = array(' -- '),
$entities_to = array(' – ');
public function __construct (array $entities = array()) {
foreach ($entities as $plain => $ent) {
$this->entities_from[] = $plain;
$this->entities_to[] = $ent;
}
}
public function run ($text) {
$this->text =& $text;
$this->_escape_init();
$this->_escape_comments_cdata();
$text = preg_replace ('~&(?=[^;]{7}|[^;]{0,6}&)~', '&', $text);
$this->_escape_content ('pre', 'code', 'samp', 'kbd', 'textarea');
$text = preg_replace_callback ('~>.*?<~s', array ($this, '_replace'), '>'.$text.'<');
$text = substr ($text, 1, -1);
$this->_escape_fin();
return $text;
}
private function _replace (array $matches) {
$matches[0] = str_replace ($this->entities_from, $this->entities_to, $matches[0]);
$matches[0] = preg_replace ('~ \.{3,}~', ' …', $matches[0]);
$matches[0] = preg_replace ('~\.{3,}~', '…', $matches[0]);
return $matches[0];
}
}
?>
<?php
ob_start (array (new Entitify (array ('---' => '—')), 'run'));
?>
Lorem ipsum dolor sit amet ..., consectetuer adipiscing elit. Donec aliquam sollicitudin mi. Suspendisse potenti.
Nullam libero ante, viverra a, suscipit non, lacinia ut, tortor. Suspendisse magna. Etiam pharetra. Morbi nulla
orci sagittis justo---Aenean volutpat tempor magna---Morbi mi massa, laoreet nec, viverra et, auctor eget, massa.
Class aptent taciti sociosqu & litora torquent per conubia nostra, per inceptos hymenaeos......