de

designoir

Entitify

Entitify.php

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(' &#8211; ');
    
    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}&)~', '&amp;', $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,}~', '&#160;&#8230;', $matches[0]);
        $matches[0] = preg_replace ('~\.{3,}~', '&#8230;', $matches[0]);
        return $matches[0];
    }
}

?>

Example #1

test.php

<?php

ob_start (array (new Entitify (array ('---' => '&#8212;')), '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......

Comments

HTML will not be interpreted.

Last changed on March 31, 2006 Dão G., 2005–2010
aggressiv akt andromeda bar beine blue efeu frontal fugaetu industriell komet land noir rost rot sonnenblume splash split winter wolke zeit