PHP-Einfach.de PHP lernen leicht gemacht

Navigation
» Startseite
» Newsübersicht
» Kontakt
» Impressum

Community
» Forum
» Gästebuch

Tutorial
» PHP Tutorial
» MySQL Tutorial
» PHP
» MySQL
» Codeschnipsel

Downloads
» Einführung
» Scripts
» Command Board

Sonstiges
» md5-Generator
» Generator
» Wissenswertes

PHP lernen


Dieses Projekt wird unterstützt von
Lichteffekte Shop

 
Partner:
PHP Forum
Mathe Nachhilfe
Suchmaschinenoptimierung

Highlight Klasse

Zurück zur Übersicht

Diese Klasse hier dient zum hervorheben von Html und PHP Code.

 PHP 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
<?php
/**
 * Highlight Klasse
 * @version 0.0
 * @author benjamin <spam@abba-skript.de>
 *
 * Diese Klasse dient zum hervorheben von Html und PHP Code.
 */
class highlight {
    private 
$ersetzen = array(array());
    private 
$codes = array(array());

    
/**
     * PHP Code einfärben
     * @param string $text Text zum einfärben
     * @return string eingefärbter PHP Code wird zurück gegeben. 
     */
    
public function php($text) {
        
$text highlight_string($textTRUE);
        return 
$text;
    }

    
/**
     * HTML Code einfärben
     * @param string $text Text zum einfärben
     * @return string eingefärbter HTML Code wird zurück gegeben. 
     */
    
public function html($text) {
        
$text htmlspecialchars($text);
        
$this -> ersetzen("&lt;""<span style=\"color: green;\">&lt;");
        
$this -> ersetzen("&gt;""&gt;</span>");
        
$this -> codes("/&quot;(.*)&quot;/Usi""<span style=\"color: red;\">&quot;\\1&quot;</span>");
        
$this -> codes("/&lt;!--(.*)--&gt;/Usi""<span style=\"color: blue;\">&lt;!--\\1--&gt;</span>");
        
$this -> ersetzen("\t""&nbsp;&nbsp;&nbsp;&nbsp;");
        
$text str_replace($this -> ersetzen[0], $this -> ersetzen[1], $text);
        
$text preg_replace($this -> codes[0], $this -> codes[1], $text);
        return 
"<code>".nl2br($text)."</code>";
    }

    private function 
ersetzen($name$wert) {
        if (!empty(
$name)) {
                
$anzahl = (count($this -> ersetzenCOUNT_RECURSIVE)/2-1);
                
$this -> ersetzen[0][$anzahl] = $name;
                
$this -> ersetzen[1][$anzahl] = $wert;
            }
    }

    private function 
codes($v1$v2) {
        if (!empty(
$v1) and !empty($v2)) {
                
$anzahl = (count($this -> codesCOUNT_RECURSIVE)/2-1);
                
$this -> codes[0][$anzahl] = $v1;
                
$this -> codes[1][$anzahl] = $v2;
            }
    }
}


Nutzung
 PHP 
1:
2:
3:
4:
5:
<?php
$h 
= new highlight();
echo 
$h -> html(file_get_contents("http://www.php-einfach.de"));
echo 
"<hr />";
echo 
$h -> php(file_get_contents(__FILE__));


Das ganze sieht dann z.B. so aus:


Kommentare

Zurück zur Übersicht

Autor Gast

News
13.08 - » Spam im Gästebuch
Endlich Schluss mit dem Spam

08.12 - » Clanletter 2.0
Clanletter wurde komplett neu programmiert

01.09 - » Command Board 1.0 - 2.0
Das Command Board 1.0 Beta 2.0 ist erschienen


Mehr

Forum
» Entwickler Forum

» insert nach login

» Tabellenzelle zu groß







© PHP-Einfach.de 2003 - 2012