Gast
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 ( $text , TRUE );
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 ( "<" , "<span style=\"color: green;\"><" );
$this -> ersetzen ( ">" , "></span>" );
$this -> codes ( "/"(.*)"/Usi" , "<span style=\"color: red;\">"\\1"</span>" );
$this -> codes ( "/<!--(.*)-->/Usi" , "<span style=\"color: blue;\"><!--\\1--></span>" );
$this -> ersetzen ( "\t" , " " );
$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 -> ersetzen , COUNT_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 -> codes , COUNT_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:
Post wurde schon 3x editiert, das letzte mal am 16.05.2010 um 23:33 von
16.05.2010, 21:20
stex
Mitglied
Sehr guter User
Dabei seit: 14.04.2009
Herkunft: Hannover
Posts: 650
Hast mal ne Demo parat ?
16.05.2010, 21:47
Profil |
PM |
E-Mail
B.C.
Mitglied
Sehr guter User
Dabei seit: 04.02.2009
Herkunft: Niedersachsen
Posts: 797
Super Ding!
Habs sofort in mein CMS als optionalen Post-Filter eingebaut. Jedoch wird dabei die Einrückung nicht
beachtet, deshalb hab ich bei mir das hinzugefügt:
PHP
1:
2:
<?
$this -> ersetzen ( ' ' , ' ' );
Finde ich dann persönlich besser...
Post wurde schon 1x editiert, das letzte mal am 22.05.2010 um 14:15 von B.C.
22.05.2010, 14:15
Profil |
PM |
E-Mail
Gast
wenn Tabulatoren verwendet werden gibt es kein Problem aber ok, ich werde es fixen.
22.05.2010, 14:50
FalkenaugeMihawk
Mitglied
Perfekter User
Dabei seit: 05.06.2010
Herkunft: Schweiz
Posts: 2619
Gibt es auch ne Möglichkeit, CSS zu "highlighten"?^^
Post wurde schon 1x editiert, das letzte mal am 27.07.2010 um 02:17 von FalkenaugeMihawk
27.07.2010, 02:17
Profil |
PM |
E-Mail
Teralios
Moderator
Perfekter User
Dabei seit: 18.09.2005
Herkunft: Berlin
Posts: 2542
27.07.2010, 08:28
Profil |
PM |
E-Mail