-
Notifications
You must be signed in to change notification settings - Fork 11
/
redletter.php
121 lines (102 loc) · 3.45 KB
/
redletter.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* This class is used to color Christ's words in red.
* Important Note: This class assumes proper placement of “ ” (Not " ") to mark the verses
*
* @author jayarathina
* @version 3.0
*
*/
class RedLetter {
private $inst = [];
/**
* @param string $inst - Instructions for coloring from DB; (See switch case in colorRedLetter function)
*
*/
function __construct($insts) {
foreach ($insts as $val) {
if(empty($val['id_to'])){
$val['id_to'] = $val['id_from'];
}
$this->inst += array_fill($val['id_from'], $val['id_to']-$val['id_from']+1, $val['inst']);
}
}
/**
* @param string $vs - Verse text
* @param string $vno - Verse number
* @return string - Colored String
*/
function colorRedLetter($vs, $vno) {
if(! isset($this->inst[$vno])) return $vs;
$rt=''; //Return value
if(strpos($vs, BLIB_VERSE_NUMBER_END) !== false){
$vs_t = explode(BLIB_VERSE_NUMBER_END, $vs);
$rt= $vs_t[0] . BLIB_VERSE_NUMBER_END; //Coupled verses start
$vs = $vs_t[1];
}
$inst = $this->inst[$vno];
switch ($inst) {//What should be colored?
case '1': //Only text within first double quotes
case '2': //Only text within second double quotes
case '1,3': //First and third quote 49021031; 52021015-52021017
$v = explode('”', $vs);
$ct = explode(',', $inst);
foreach ($ct as $vl) {
$vt = $vl-1;
$v[$vt] = $this->defaultAllQuotes ($v[$vt] . '”');
$v[$vt] = rtrim($v[$vt], '”');//Remove the extra quote added in the prev line
}
$rt .= implode('”', $v);
break;
default://All text within double quotes
$rt .= $this->defaultAllQuotes ($vs);
break;
}
$rt = str_replace(BLIB_RED_LTR_END . '”', '”' . BLIB_RED_LTR_END, $rt);
$rt = str_replace('“'.BLIB_RED_LTR_START, BLIB_RED_LTR_START.'“', $rt);
return $rt;
}
/**
* @param string $vs - Verse text; Note that this class assumes proper placement of “ ” (Not " ") to mark the verses
* @return string - String with start and end positions to be tagged
*/
private function defaultAllQuotes($vs) {
//Start and end marker to mark the begining and en
$openQc = substr_count($vs, '“');
$closeQc = substr_count($vs, '”');
if($openQc <= 1 && $closeQc <= 1){
//There is one or less than one open and close quote.
if($openQc === 0){//If open quotes is missing, then prepend
$vs = BLIB_RED_LTR_START . $vs;
}else{
$vs = str_replace('“', '“'.BLIB_RED_LTR_START , $vs);
}
if($closeQc === 0){//If close quotes is missing, then append
$vs .= BLIB_RED_LTR_END;
}else{
$vs = str_replace('”', BLIB_RED_LTR_END.'”', $vs);
}
$oQLoc = strpos($vs, '“');
$cQLoc = strpos($vs, '”');
if( ($oQLoc > $cQLoc) && ($openQc === 1) && ($closeQc === 1) ){
//Quotes are opposite, eg: [ ..some text” said he, and then he continued “loremp ipsum... ]
$vs = BLIB_RED_LTR_START.$vs.BLIB_RED_LTR_END;
}
}else{
$vss = explode('”', $vs);
foreach ($vss as $key => $value) {
if($key === count($vss)-1 ){//In the last segment
$openQc = substr_count($value, '“');
if($openQc === 0){//50005041 //if it does not contains an open quote
break;// then don't color the last segment
}
}
if(! empty($value))//if verse ends with ”, then last value will be empty
$vss[$key] = $this->defaultAllQuotes($value . '”');
}
$vs = implode('”', $vss);
}
$vs = str_replace('””', '”', $vs);
return $vs;
}
}