Highlight HTML en XML
n.a.v. de highlight SQL functie heb ik nu ook een highlight HTML functie gebouwd.
Op en aanmerkingen zijn wederom erg welkom!
Enjoy!
Voorbeeld: http://www.pholeron.nl/projects/highlight_html/
Gesponsorde koppelingen
PHP script bestanden
15 reacties op 'Highlight HTML en XML'
Gesponsorde koppelingen
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
// Parse UBB code to HTML
function ubbToHtml($string, $bAddDefaultCss = false)
{
$result = '';
// Slice string into UBB blocks
$expr = '/(\[((\n|.)*?)\]((\n|.)*?)\[\/\\2\])|(<\?((\n|.)*?)\?>)|(.)/i';
preg_match_all($expr, $string, $matches);
for($i = 0; $i < sizeof($matches[0]); $i++)
{
if(strcasecmp($match = $matches[0][$i], "") !== 0)
{
if(preg_match('/\[code\]((\n|.)*?)\[\/code\]/i', $match)) // General code block
{
$result .= '<code>' . stringToHtml(trim(substr($match, 6, -7))) . '</code>';
}
elseif(preg_match('/\[css\]((\n|.)*?)\[\/css\]/i', $match)) // HTML
{
$result .= highlight_css(trim(substr($match, 6, -7)), $bAddDefaultCss);
}
elseif(preg_match('/\[html\]((\n|.)*?)\[\/html\]/i', $match)) // HTML
{
$result .= highlight_html(trim(substr($match, 6, -7)), $bAddDefaultCss);
}
elseif(preg_match('/\[js\]((\n|.)*?)\[\/js\]/i', $match)) // Javascript
{
$result .= highlight_js(trim(substr($match, 4, -5)), $bAddDefaultCss);
}
elseif(preg_match('/\[php\]((\n|.)*?)\[\/php\]/i', $match)) // PHP
{
$result .= highlight_php(trim(substr($match, 5, -6)), $bAddDefaultCss);
}
elseif(preg_match('/\[sql\]((\n|.)*?)\[\/sql]/i', $match)) // SQL
{
$result .= highlight_sql(trim(substr($match, 5, -6)), $bAddDefaultCss);
}
elseif(preg_match('/\[vb\]((\n|.)*?)\[\/js\]/i', $match)) // Visual Basic
{
$result .= highlight_vb(trim(substr($match, 4, -5)));
}
elseif(preg_match('/\[xml\]((\n|.)*?)\[\/js\]/i', $match)) // XML
{
$result .= highlight_xml(trim(substr($match, 5, -6)), $bAddDefaultCss);
}
elseif(preg_match('/\[quote\]((\n|.)*?)\[\/quote\]/i', $match)) // Some quote
{
$result .= '<blockquote>' . stringToHtml(trim(substr($match, 7, -8))) . '</blockquote>';
}
else // Inline tags
{
// Escape HTML syntax
$match = stringToHtml($match);
// Bold
$match = preg_replace('/\[b\]((\n|.)*?)\[\/b\]/i', '<b>\\1</b>', $match);
$match = preg_replace('/\[bold\]((\n|.)*?)\[\/bold\]/i', '<b>\\1</b>', $match);
// Italic
$match = preg_replace('/\[i\]((\n|.)*?)\[\/i\]/i', '<i>\\1</i>', $match);
$match = preg_replace('/\[italic\]((\n|.)*?)\[\/italic\]/i', '<i>\\1</i>', $match);
// Underline
$match = preg_replace('/\[u\]((\n|.)*?)\[\/u\]/i', '<u>\\1</u>', $match);
$match = preg_replace('/\[underline\]((\n|.)*?)\[\/underline\]/i', '<u>\\1</u>', $match);
// Strike
$match = preg_replace('/\[s\]((\n|.)*?)\[\/s\]/i', '<strike>\\1</strike>', $match);
$match = preg_replace('/\[strike\]((\n|.)*?)\[\/strike\]/i', '<strike>\\1</strike>', $match);
// Sup
$match = preg_replace('/\[sup\]((\n|.)*?)\[\/sup\]/i', '<sup>\\1</sup>', $match);
// Sub
$match = preg_replace('/\[sub\]((\n|.)*?)\[\/sub\]/i', '<sub>\\1</sub>', $match);
// Color
$match = preg_replace('/\[color="(.*?)"\]((\n|.)*?)\[\/color\]/i', '<span style="color: \\1;">\\2</span>', $match);
$match = preg_replace('/\[color=(.*?)\]((\n|.)*?)\[\/color\]/i', '<span style="color: \\1;">\\2</span>', $match);
// Google link
$match = preg_replace('/\[google\]((\n|.)*?)\[\/google\]/i', '<a href="http://www.google.nl/search?q=\\1" target="_new">\\1</a>', $match);
// Image
$match = preg_replace('/\[img align="(.*?)"\](.*?)\[\/img\]/i', '<img align="\\1" alt="\\2" border="0" src="\\2">', $match);
$match = preg_replace('/\[img align=(.*?)\](.*?)\[\/img\]/i', '<img align="\\1" alt="\\2" border="0" src="\\2">', $match);
$match = preg_replace('/\[img\](.*?)\[\/img\]/i', '<img alt="\\1" border="0" src="\\1">', $match);
$match = preg_replace('/\[image align="(.*?)"\](.*?)\[\/image\]/i', '<img align="\\1" alt="\\2" border="0" src="\\2">', $match);
$match = preg_replace('/\[image align=(.*?)\](.*?)\[\/image\]/i', '<img align="\\1" alt="\\2" border="0" src="\\2">', $match);
$match = preg_replace('/\[image\](.*?)\[\/image\]/i', '<img alt="\\1" border="0" src="\\1">', $match);
// Parse URLs
$match = preg_replace('/\[url="(.*?)"\](.*?)\[\/url\]/i', '<a href="\\1" target="_new">\\2</a>', $match); // [url="http://www.domain.tld"]label[/url]
$match = preg_replace('/\[url=(.*?)\](.*?)\[\/url\]/i', '<a href="\\1" target="_new">\\2</a>', $match); // [url=http://www.domain.tld]label[/url]
$match = preg_replace('/\[url\](.*?)\[\/url\]/i', '<a href="\\1" target="_new">\\1</a>', $match); // [url]http://www.domain.tld[/url]
$result .= $match;
}
}
}
return $result;
}
?>
// Parse UBB code to HTML
function ubbToHtml($string, $bAddDefaultCss = false)
{
$result = '';
// Slice string into UBB blocks
$expr = '/(\[((\n|.)*?)\]((\n|.)*?)\[\/\\2\])|(<\?((\n|.)*?)\?>)|(.)/i';
preg_match_all($expr, $string, $matches);
for($i = 0; $i < sizeof($matches[0]); $i++)
{
if(strcasecmp($match = $matches[0][$i], "") !== 0)
{
if(preg_match('/\[code\]((\n|.)*?)\[\/code\]/i', $match)) // General code block
{
$result .= '<code>' . stringToHtml(trim(substr($match, 6, -7))) . '</code>';
}
elseif(preg_match('/\[css\]((\n|.)*?)\[\/css\]/i', $match)) // HTML
{
$result .= highlight_css(trim(substr($match, 6, -7)), $bAddDefaultCss);
}
elseif(preg_match('/\[html\]((\n|.)*?)\[\/html\]/i', $match)) // HTML
{
$result .= highlight_html(trim(substr($match, 6, -7)), $bAddDefaultCss);
}
elseif(preg_match('/\[js\]((\n|.)*?)\[\/js\]/i', $match)) // Javascript
{
$result .= highlight_js(trim(substr($match, 4, -5)), $bAddDefaultCss);
}
elseif(preg_match('/\[php\]((\n|.)*?)\[\/php\]/i', $match)) // PHP
{
$result .= highlight_php(trim(substr($match, 5, -6)), $bAddDefaultCss);
}
elseif(preg_match('/\[sql\]((\n|.)*?)\[\/sql]/i', $match)) // SQL
{
$result .= highlight_sql(trim(substr($match, 5, -6)), $bAddDefaultCss);
}
elseif(preg_match('/\[vb\]((\n|.)*?)\[\/js\]/i', $match)) // Visual Basic
{
$result .= highlight_vb(trim(substr($match, 4, -5)));
}
elseif(preg_match('/\[xml\]((\n|.)*?)\[\/js\]/i', $match)) // XML
{
$result .= highlight_xml(trim(substr($match, 5, -6)), $bAddDefaultCss);
}
elseif(preg_match('/\[quote\]((\n|.)*?)\[\/quote\]/i', $match)) // Some quote
{
$result .= '<blockquote>' . stringToHtml(trim(substr($match, 7, -8))) . '</blockquote>';
}
else // Inline tags
{
// Escape HTML syntax
$match = stringToHtml($match);
// Bold
$match = preg_replace('/\[b\]((\n|.)*?)\[\/b\]/i', '<b>\\1</b>', $match);
$match = preg_replace('/\[bold\]((\n|.)*?)\[\/bold\]/i', '<b>\\1</b>', $match);
// Italic
$match = preg_replace('/\[i\]((\n|.)*?)\[\/i\]/i', '<i>\\1</i>', $match);
$match = preg_replace('/\[italic\]((\n|.)*?)\[\/italic\]/i', '<i>\\1</i>', $match);
// Underline
$match = preg_replace('/\[u\]((\n|.)*?)\[\/u\]/i', '<u>\\1</u>', $match);
$match = preg_replace('/\[underline\]((\n|.)*?)\[\/underline\]/i', '<u>\\1</u>', $match);
// Strike
$match = preg_replace('/\[s\]((\n|.)*?)\[\/s\]/i', '<strike>\\1</strike>', $match);
$match = preg_replace('/\[strike\]((\n|.)*?)\[\/strike\]/i', '<strike>\\1</strike>', $match);
// Sup
$match = preg_replace('/\[sup\]((\n|.)*?)\[\/sup\]/i', '<sup>\\1</sup>', $match);
// Sub
$match = preg_replace('/\[sub\]((\n|.)*?)\[\/sub\]/i', '<sub>\\1</sub>', $match);
// Color
$match = preg_replace('/\[color="(.*?)"\]((\n|.)*?)\[\/color\]/i', '<span style="color: \\1;">\\2</span>', $match);
$match = preg_replace('/\[color=(.*?)\]((\n|.)*?)\[\/color\]/i', '<span style="color: \\1;">\\2</span>', $match);
// Google link
$match = preg_replace('/\[google\]((\n|.)*?)\[\/google\]/i', '<a href="http://www.google.nl/search?q=\\1" target="_new">\\1</a>', $match);
// Image
$match = preg_replace('/\[img align="(.*?)"\](.*?)\[\/img\]/i', '<img align="\\1" alt="\\2" border="0" src="\\2">', $match);
$match = preg_replace('/\[img align=(.*?)\](.*?)\[\/img\]/i', '<img align="\\1" alt="\\2" border="0" src="\\2">', $match);
$match = preg_replace('/\[img\](.*?)\[\/img\]/i', '<img alt="\\1" border="0" src="\\1">', $match);
$match = preg_replace('/\[image align="(.*?)"\](.*?)\[\/image\]/i', '<img align="\\1" alt="\\2" border="0" src="\\2">', $match);
$match = preg_replace('/\[image align=(.*?)\](.*?)\[\/image\]/i', '<img align="\\1" alt="\\2" border="0" src="\\2">', $match);
$match = preg_replace('/\[image\](.*?)\[\/image\]/i', '<img alt="\\1" border="0" src="\\1">', $match);
// Parse URLs
$match = preg_replace('/\[url="(.*?)"\](.*?)\[\/url\]/i', '<a href="\\1" target="_new">\\2</a>', $match); // [url="http://www.domain.tld"]label[/url]
$match = preg_replace('/\[url=(.*?)\](.*?)\[\/url\]/i', '<a href="\\1" target="_new">\\2</a>', $match); // [url=http://www.domain.tld]label[/url]
$match = preg_replace('/\[url\](.*?)\[\/url\]/i', '<a href="\\1" target="_new">\\1</a>', $match); // [url]http://www.domain.tld[/url]
$result .= $match;
}
}
}
return $result;
}
?>
.. alleen zijn de meeste highlight_???() nog niet goed genoeg uitgewerkt ;)
maar als ie af is wordt ie hier natuurlijk ook gepost!
Om te reageren heb je een account nodig en je moet ingelogd zijn.
- Details
Door:
Martijn Wieringa- 6 jaar geleden
- 663 x bekeken
- Labels
- Geen tags toegevoegd.
- PHP scripts opties
- Image manipulation
- Nieuwste PHP scripts
- PHP script toevoegen


PHP hulp
0 seconden vanaf nu