-
Notifications
You must be signed in to change notification settings - Fork 0
/
font-chooser-parser.php
86 lines (73 loc) · 1.91 KB
/
font-chooser-parser.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
<?php
/**
* Content parser
*/
class FontChooserParser
{
// list of supported fonts
private static $s_asSupportedFonts = array
(
"Arial",
"Arial Black",
"Comic Sans MS",
"Courier New",
"Georgia1",
"Impact",
"Lucida Console",
"Tahoma",
"Times New Roman",
"Trebuchet MS1",
"Verdana",
"Symbol",
"Webdings",
"Wingdings"
);
/**
* Parse a content string and make font-span replacements
*
* @param string $sContent the content of the page being displayed
* @return string the (possibly modified) content
*/
public function parse(&$sContent)
{
// loop content and look for simple font tags
$nPosition = 0;
$sModified = "";
for ($nContent = strlen($sContent), $nPosition = 0; $nPosition < $nContent; $nPosition ++)
{
if ($sContent{$nPosition} == '[')
{
$nPosition ++;
// make sure this isn't the end
if ($nPosition >= $nContent)
{
$sModified .= '[';
break;
}
// grab the following section of the content for comparison
$sPossible = substr($sContent, $nPosition, 32);
foreach (FontChooserParser::$s_asSupportedFonts as $sFont)
{
if (!strncmp($sPossible, $sFont, strlen($sFont)))
{
// supported font match
$sModified .= '<span style="font-family: '.$sFont.';">';
$nPosition += strlen($sFont) + 1;
while (($c = $sContent{$nPosition}) != ']')
{
if (($c == '\\') && ($sContent{$nPosition + 1} == ']'))
$sModified .= $sContent{++$nPosition};
else
$sModified .= $c;
$nPosition ++;
}
$sModified .= '</span>';
}
}
}
else
$sModified .= $sContent{$nPosition};
}
return $sModified;
}
}