· 7 years ago · Feb 15, 2019, 12:12 AM
1//--------------------------------------------------------------------
2// FUNCTION:
3// KT_MXSearch
4//
5// DESCRIPTION:
6// KT_MXSearch constructor
7//
8// ARGUMENTS:
9// searchName - Name of the object
10//
11// RETURNS:
12// nothing
13//--------------------------------------------------------------------
14
15function KT_MXSearch() {
16}
17
18//--------------------------------------------------------------------
19// FUNCTION:
20// setConnection
21//
22// DESCRIPTION:
23// Sets the connection and the connection type (database type)
24//
25// ARGUMENTS:
26// connection - connection name
27// databaseType - database type
28//
29// RETURNS:
30// nothing
31//--------------------------------------------------------------------
32
33function setConnection(&$connection , $databaseType) {
34 $this->connection = $connection;
35 $this->databaseType = $databaseType;
36}
37
38
39
40function setCache($cacheTable, $refreshCacheDelay) {
41 $this->cacheTable = $cacheTable;
42 $this->refreshCacheDelay = $refreshCacheDelay*60;
43}
44
45function setTempTable($tmpTable) {
46 $this->tmpTable = $tmpTable;
47}
48
49function setTables($config) {
50 $this->tables = $config->Tables;
51}
52function setSearchType($searchType) {
53 $this->searchType = $searchType;
54}
55
56
57//--------------------------------------------------------------------
58// FUNCTION:
59// checkTableExists()
60//
61// DESCRIPTION:
62// Verifies the existence of a table in the database
63//
64// ARGUMENTS:
65// tableName - name of the table
66//
67// RETURNS:
68// true or false
69//--------------------------------------------------------------------
70
71function checkTableExists($tableName){
72 //check if table exists
73 $KT_sql = "SELECT * FROM $tableName";
74 $testRecord = mysql_query($KT_sql, $this->connection);
75 if (mysql_error()) {
76 return false;
77 } else {
78 return true;
79 }
80}
81
82//--------------------------------------------------------------------
83// FUNCTION:
84// createTmpTable
85//
86// DESCRIPTION:
87// Creates the table that holds the cache refresh information
88//
89// ARGUMENTS:
90// none
91//
92// RETURNS:
93// nothing
94//--------------------------------------------------------------------
95
96function createTmpTable(){
97 //create settings table
98 $KT_sql = "CREATE TABLE ".$this->tmpTable."(
99 lastupd_tmp TEXT NOT NULL
100 );";
101 mysql_query($KT_sql, $this->connection) or die(mysql_error());
102 //insert record
103 $this->initTmpTable();
104}
105
106//--------------------------------------------------------------------
107// FUNCTION:
108// updateTmpTable
109//
110// DESCRIPTION:
111// Updates the table that holds the cache refresh information
112//
113// ARGUMENTS:
114// none
115//
116// RETURNS:
117// true or false
118//--------------------------------------------------------------------
119
120function updateTmpTable(){
121 $now = date("Y/m/d H:i:s");
122 $KT_sql = "update ".$this->tmpTable." SET lastupd_tmp = '$now'";
123 mysql_query($KT_sql, $this->connection) or die(mysql_error());
124 return true;
125}
126
127
128function initTmpTable() {
129 $KT_sql = "INSERT INTO ".$this->tmpTable." (lastupd_tmp) VALUES('1970/01/01 00:00:00') ;";
130 mysql_query($KT_sql, $this->connection) or die(mysql_error());
131}
132//--------------------------------------------------------------------
133// FUNCTION:
134// checkCacheExpired
135//
136// DESCRIPTION:
137// Checks if the cache has expired
138//
139// ARGUMENTS:
140// none
141//
142// RETURNS:
143// true or false
144//--------------------------------------------------------------------
145
146function checkCacheExpired() {
147 $KT_sql = "select lastupd_tmp from ".$this->tmpTable;
148 $mxs_result = mysql_query($KT_sql, $this->connection) or die(mysql_error());
149 $row_mxs_result = mysql_fetch_assoc($mxs_result);
150 if ($row_mxs_result['lastupd_tmp']) {
151 $date = $row_mxs_result['lastupd_tmp'];
152 if (strtotime(date("Y/m/d H:i:s")) - strtotime($date) > $this->refreshCacheDelay) {
153 return true;
154 } else {
155 return false;
156 }
157 } else {
158 $this->initTmpTable();
159 return false;
160 }
161}
162
163//--------------------------------------------------------------------
164// FUNCTION:
165// createCacheTable
166//
167// DESCRIPTION:
168// Creates the cache table
169//
170// ARGUMENTS:
171// none
172//
173// RETURNS:
174// nothing
175//--------------------------------------------------------------------
176
177function createFulltextIndex($dbType) {
178 if ($dbType=="MySQL") {
179 for ($i=1;$i<=5;$i++) {
180 $KT_sql = 'ALTER TABLE `'.$this->cacheTable.'` DROP INDEX `idx_description`'.$i;
181 mysql_query($KT_sql, $this->connection);
182 $KT_sql = 'create fulltext index idx_description'.$i.' on '.$this->cacheTable.' (col'.$i.'_cah);';
183 mysql_query($KT_sql, $this->connection) or die(mysql_error()."<br>Please check that your database supports fulltext search");
184 }
185 }
186}
187
188function createCacheTable() {
189 $KT_sql = "drop table ".$this->cacheTable;
190 mysql_query($KT_sql, $this->connection);
191 $KT_sql = sprintf($this->sql['create_cache'], $this->cacheTable);
192 mysql_query($KT_sql, $this->connection) or die(mysql_error());
193
194 if ($this->searchType=='fulltext') {
195 $this->createFulltextIndex($this->databaseType);
196 }
197
198}
199
200//--------------------------------------------------------------------
201// FUNCTION:
202// fillCacheTable
203//
204// DESCRIPTION:
205// Fills the cache table
206//
207// ARGUMENTS:
208// none
209//
210// RETURNS:
211// nothing
212//--------------------------------------------------------------------
213
214function fillCacheTable() {
215 $this->updateTmpTable();
216 $this->createCacheTable();
217
218 if (is_array($this->tables)){
219 foreach($this->tables as $tableKey => $crtTable){
220 $columnsString = array();
221
222 $KT_TableName = $tableKey;
223
224 //for each column...
225
226 if (is_array($crtTable['searchColumns'])){
227 $tmpColumnArr = array();
228 foreach($crtTable['searchColumns'] as $fieldKey => $fieldValue) {
229 $tmpColumnArr[$fieldValue][] = "`".$fieldKey."`";
230 }
231 for ($relIdx=1;$relIdx<=5;$relIdx++) {
232 if (isset($tmpColumnArr[$relIdx])) {
233 $columnsString[$relIdx] = implode(",",$tmpColumnArr[$relIdx]);
234 } else {
235 unset ($columnsString[$relIdx]);
236 }
237
238 }
239
240 }else {
241 die('No search columns defined in table '.$KT_TableName.'!');
242 }
243 //end columns
244
245 //set the columns values
246 $cacheTile = $crtTable['resultTitle'];
247 $cacheDesc = $crtTable['resultDesc'];
248 $cacheURL = $crtTable['pageName'];
249 $cacheImportance = $crtTable['TableImportance'];
250 if (isset($crtTable['AditionalCondition'])) {
251 $cacheAditionalCond = $crtTable['AditionalCondition'];
252 } else {
253 unset($cacheAditionalCond);
254 }
255
256 //compute result url parameters
257 $paramValue = $crtTable['pageParam'];
258
259 $KT_sql = sprintf($this->sql['select'], $cacheTile, $cacheDesc, $paramValue, $cacheImportance);
260 for ($relIdx=1;$relIdx<=5;$relIdx++) {
261 if (isset($columnsString[$relIdx])) {
262 $KT_sql .= ", ".$columnsString[$relIdx];
263 }
264 }
265 $KT_sql .= " FROM ".$KT_TableName;
266 if (isset($cacheAditionalCond) && $cacheAditionalCond != '') {
267 $KT_sql .= " WHERE $cacheAditionalCond ";
268 }
269
270 $KT_results = mysql_query($KT_sql, $this->connection) or die(mysql_error()."<br>".$KT_sql);
271 $row_KT_results = mysql_fetch_assoc($KT_results);
272
273 do {
274 $cacheCol = array();
275 $col_cah = array();
276 $title_cah = addslashes(strip_tags($row_KT_results['title_cah']));
277 $shortdesc_cah = addslashes(strip_tags($row_KT_results['shortdesc_cah']));
278 for ($relIdx=1;$relIdx<=5;$relIdx++) {
279 if (isset($tmpColumnArr[$relIdx])) {
280 $cacheCol[$relIdx] = "";
281 for ($colIdx=0;$colIdx<count($tmpColumnArr[$relIdx]);$colIdx++) {
282 $cacheCol[$relIdx] .= "rn".$row_KT_results[str_replace("`", "", $tmpColumnArr[$relIdx][$colIdx])];
283 }
284 }
285 }
286 $KT_colString = ''; $KT_valueString = '';
287 for ($relIdx=1;$relIdx<=5;$relIdx++) {
288 if (isset($cacheCol[$relIdx])) {
289 $col_cah[$relIdx] = addslashes(strip_tags($cacheCol[$relIdx]));
290 $KT_colString .= ", col".$relIdx."_cah";
291 $KT_valueString .= ", '".$col_cah[$relIdx]."'";
292 if ($this->databaseType=="PostgreSQL" && $this->searchType=="fulltext") {
293 $KT_colString .= ", col".$relIdx."_vect_cah";
294 $KT_valueString .= ", to_tsvector('".$col_cah[$relIdx]."')";
295 }
296 } else {
297 $col_cah[$relIdx] = "";
298 }
299 }
300 $url_cah = addslashes($cacheURL.$row_KT_results['url_cah']);
301 $importance_cah = $this->importanceArray[$cacheImportance];
302 $KT_sql = "INSERT INTO ".$this->cacheTable." (title_cah, shortdesc_cah".$KT_colString.", importance_cah, url_cah) values ('$title_cah', '$shortdesc_cah'".$KT_valueString.", $importance_cah, '$url_cah');";
303 mysql_query($KT_sql) or die(mysql_error());
304 } while ($row_KT_results = mysql_fetch_assoc($KT_results));
305
306 }//foreach table
307 }else {die('No search tables defined!');}
308}
309
310function getRecordset($start = null, $max = null) {
311 $kt_searchWhere = $this->getWhereCondition();
312 $kt_searchOrder = $this->getOrderBy();
313 $kt_columns = $this->getSearchColumns();
314 $KT_sql = sprintf("SELECT * %s FROM %s WHERE %s ORDER BY %s", $kt_columns, $this->cacheTable, $kt_searchWhere, $kt_searchOrder);
315 $KT_result = mysql_query($KT_sql, $this->connection) or die(mysql_error());
316 $this->totalRows = mysql_num_rows($KT_result);
317 if (isset($start) && isset($max)) {
318 $KT_result = mysql_query($KT_sql." LIMIT ".$start.", ".$max, $this->connection) or die(mysql_error());
319 }
320 return $KT_result;
321}
322
323function getTotalRows() {
324 return $this->totalRows;
325}
326
327//--------------------------------------------------------------------
328// FUNCTION:
329// refreshCache
330//
331// DESCRIPTION:
332// Checks if the cache is expired and refreshes the cache if necesary
333//
334// ARGUMENTS:
335// none
336//
337// RETURNS:
338// nothing
339//--------------------------------------------------------------------
340
341function refreshCache(){
342 if (!$this->checkTableExists($this->tmpTable)){
343 $this->createTmpTable($this->tmpTable);
344 }
345 if ($this->checkCacheExpired()){
346 $this->fillCacheTable();
347 }
348}
349
350//--------------------------------------------------------------------
351// FUNCTION:
352// computeAll
353//
354// DESCRIPTION:
355// Checks the type of the search and performs the right search
356//
357// ARGUMENTS:
358// searchFor - expression to search for
359//
360// RETURNS:
361// nothing
362//--------------------------------------------------------------------
363
364function computeAll($searchFor){
365 $this->refreshCache();
366 $this->setSearchTerm($searchFor);
367
368 if ($this->searchType=='normal') {
369 $this->computeNormalSearch();
370 }
371 if ($this->searchType=='fulltext') {
372 $this->computeFullSearch($this->databaseType);
373 }
374 if ($this->searchType=='boolean fulltext') {
375 $this->computeMySQLFullSearch($this->searchType);
376 }
377}
378
379//--------------------------------------------------------------------
380// FUNCTION:
381// setSearchTerm
382//
383// DESCRIPTION:
384// Sets the expression to search for
385//
386// ARGUMENTS:
387// searchFor - the expression
388//
389// RETURNS:
390// nothing
391//--------------------------------------------------------------------
392
393function setSearchTerm($searchFor){
394 if (get_magic_quotes_gpc()) {
395 $keywords = @$_GET[$searchFor];
396 } else {
397 $keywords = addslashes(@$_GET[$searchFor]);
398 }
399 $this->searchFor = stripslashes(strtolower($keywords));
400}
401
402//--------------------------------------------------------------------
403// FUNCTION:
404// computeNormalSearch
405//
406// DESCRIPTION:
407// Computes the SQL for the normal search
408//
409// ARGUMENTS:
410//
411// RETURNS:
412//
413//--------------------------------------------------------------------
414
415function computeNormalSearch() {
416 $searchFor = $this->searchFor;
417 $like = $this->sql[$this->databaseType]['like'];
418 if ($searchFor) {
419 $searchCond = explode(" and ", $searchFor);
420 for ($condIdx=0;$condIdx<count($searchCond);$condIdx++) {
421 if ($searchCond[$condIdx]!='') {
422 if ($condIdx!=0) {
423 $this->whereCondition .= " AND (";
424 } else {
425 $this->whereCondition .= " (";
426 }
427 $searchCond[$condIdx] = str_replace(" or "," ", $searchCond[$condIdx]);
428 $expr = "/"([^"]*)"/m";
429 $matches = array();
430 preg_match_all($expr, $searchCond[$condIdx], $matches);
431 $searchCond[$condIdx] = preg_replace($expr, "", $searchCond[$condIdx]);
432 $searchWords = explode(" ", $searchCond[$condIdx]);
433 if (is_array($matches[1])) {
434 foreach ($matches[1] as $key=>$value) {
435 array_push($searchWords, $value);
436 }
437 }
438 $first = true;
439 for ($i=0;$i<count($searchWords);$i++) {
440 if ($searchWords[$i]!='') {
441 if (!$first) {
442 $this->whereCondition .= " OR (";
443 } else {
444 $this->whereCondition .= " (";
445 }
446 $first = false;
447 $this->whereCondition .= " col1_cah $like '%".addslashes($searchWords[$i])."%' ";
448 for ($relIdx=2; $relIdx <= 5; $relIdx++) {
449 $this->whereCondition .= " OR col".$relIdx."_cah $like '%".addslashes($searchWords[$i])."%' ";
450 if ($relIdx==5) $this->whereCondition .= ") ";
451 }
452 }
453 }
454 $this->whereCondition .= " ) ";
455 }
456 }
457 $this->orderBy .= " importance_cah DESC";
458 } else {
459 $this->whereCondition .= " 1=-1 ";
460 $this->orderBy .= " importance_cah DESC";
461 }
462}
463
464
465//--------------------------------------------------------------------
466// FUNCTION:
467// getScore
468//
469// DESCRIPTION:
470// Calculates the importance score
471//
472// ARGUMENTS:
473// text - text to search in
474// text - text to search in
475// searchFor - keywords
476// RETURNS:
477// the importance score for the keywords
478//--------------------------------------------------------------------
479
480function getScore($text)
481{
482 $searchFor = $this->searchFor;
483 $textLo = strtolower($text);
484 $expr = "/"([^"]*)"/m";
485 $matches = array();
486 preg_match_all($expr, $searchFor, $matches);
487 $searchFor = preg_replace($expr, "", $searchFor);
488 $searchFor = str_replace('"','',$searchFor);
489 $nrWords = count(explode(" ", $text));
490 $searchWords = explode(" ", $searchFor);
491 if (is_array($matches[1])) {
492 foreach($matches[1] as $key=>$value) {
493 array_push($searchWords, $value);
494 }
495 }
496 $nrSearchWords = count($searchWords);
497 $mainWordIdx = 0;
498 $mainWord = "";
499
500 if ($nrSearchWords==1) {
501 $nrOccur = substr_count($textLo, strtolower($searchFor));
502 $score = $nrOccur/$nrWords;
503 return $score;
504 } else {
505 $score = 0;
506 $allWords = true;
507 for ($i=0;$i<$nrSearchWords;$i++) {
508 if ($searchWords[$i]!='') {
509 $nrOccur = substr_count($textLo, strtolower($searchWords[$i]));
510 if (!$nrOccur) {
511 $allWords = false;
512 }
513 $score += $nrOccur/$nrWords;
514 }
515 }
516 if ($allWords) {
517 $score *= 5;
518 }
519 return $score;
520 }
521}
522
523
524//--------------------------------------------------------------------
525// FUNCTION:
526// getOrderedArray
527//
528// DESCRIPTION:
529// Makes a sort operation on the results for the normal search using the importance score
530//
531// ARGUMENTS:
532// recSet - recordset object containing the results
533// searchFor - keywords
534// RETURNS:
535// array with the results in the right order
536//--------------------------------------------------------------------
537
538function getOrderedArray($recSet, $start, $length)
539{
540 $searchFor = $this->searchFor;
541 $ordArr = array();
542 $i = 0;
543 $row_recSet = mysql_fetch_assoc($recSet);
544 do {
545 $fieldsArr = array();
546 $fieldsArr['title_cah'] = $row_recSet['title_cah'];
547 $fieldsArr['shortdesc_cah'] = $row_recSet['shortdesc_cah'];
548 $fieldsArr['url_cah'] = $row_recSet['url_cah'];
549 $score = 0;
550 for ($j=1;$j<5;$j++) {
551 $score += $this->importanceArray[$j] * $this->getScore($row_recSet['col'.$j.'_cah'], $searchFor);
552 }
553 $fieldsArr['score'] = $score * $row_recSet['importance_cah'];
554 $ordArr[$i] = $fieldsArr;
555 $i++;
556 } while ($row_recSet = mysql_fetch_assoc($recSet));
557 $ready = false;
558 $nrRec = mysql_num_rows($recSet);
559 while(!$ready) {
560 $ready = true;
561 for($i=0;$i<$nrRec-1;$i++) {
562 if ($ordArr[$i]['score']<$ordArr[$i+1]['score']) {
563 $aux = $ordArr[$i];
564 $ordArr[$i] = $ordArr[$i+1];
565 $ordArr[$i+1] = $aux;
566 $ready = false;
567 }
568 }
569 }
570 return array_slice($ordArr, $start, $length);
571}
572
573function computeFullSearch($dbType)
574{
575 $searchFor = $this->searchFor;
576 switch ($dbType) {
577 case 'MySQL' : $this->computeMySQLFullSearch($this->searchType); break;
578 }
579}
580//--------------------------------------------------------------------
581// FUNCTION:
582// computeMySQLFullSearch
583//
584// DESCRIPTION:
585// Computes the SQL for the fulltext search
586//
587// ARGUMENTS:
588// searchFor - search expression
589//
590// RETURNS:
591// nothing
592//--------------------------------------------------------------------
593
594function computeMySQLFullSearch($fullType){
595 $searchFor = $this->searchFor;
596 if ($searchFor) {
597 $searchCond = explode(" and ", $searchFor);
598 $first = true;
599 for ($condIdx=0;$condIdx<count($searchCond);$condIdx++) {
600 if ($searchCond[$condIdx]!='') {
601 if ($condIdx!=0) {
602 $this->whereCondition .= " AND (";
603 } else {
604 $this->whereCondition .= " (";
605 }
606 $searchCond[$condIdx] = str_replace(" or "," ", $searchCond[$condIdx]);
607 if (!$first) {
608 $this->orderBy .= " + ";
609 } else {
610 $this->orderBy .= " ( ";
611 }
612 $first = false;
613 $this->whereCondition .= sprintf($this->sql['MySQL']['fulltext_where'][$fullType], 1, addslashes($searchCond[$condIdx]));
614 $this->orderBy .= " (".sprintf($this->sql['MySQL']['fulltext_order'][$fullType], 1, addslashes($searchCond[$condIdx]), $this->importanceArray[1]).") ";
615 for ($relIdx=2; $relIdx <= 5; $relIdx++) {
616 $this->orderBy .= " + (".sprintf($this->sql['MySQL']['fulltext_order'][$fullType], $relIdx, addslashes($searchCond[$condIdx]), $this->importanceArray[$relIdx]).") ";
617 $this->whereCondition .= " OR ".sprintf($this->sql['MySQL']['fulltext_where'][$fullType], $relIdx, addslashes($searchCond[$condIdx]));
618 }
619 $this->whereCondition .= " ) ";
620 }
621 }
622 $this->orderBy .= ") * importance_cah ";
623 $this->searchColumns = ", ".$this->orderBy." as score ";
624 $this->orderBy .= " DESC";
625 } else {
626 $this->whereCondition = " 1=-1 ";
627 $this->orderBy = " importance_cah DESC ";
628 }
629}
630
631
632
633function getKeywords() {
634 if ($this->searchFor) {
635 return stripslashes(str_replace('"', '"', $this->searchFor));
636 } else {
637 return '';
638 }
639}
640
641//--------------------------------------------------------------------
642// FUNCTION:
643// getSearchColumns
644//
645// DESCRIPTION:
646// Gets the aditional search columns
647//
648// ARGUMENTS:
649// none
650//
651// RETURNS:
652// String with the column names or false if no aditional search columns
653//--------------------------------------------------------------------
654
655function getSearchColumns() {
656 if (isset($this->searchColumns))
657 return $this->searchColumns;
658 else
659 return false;
660}
661
662//--------------------------------------------------------------------
663// FUNCTION:
664// getWhereConditions
665//
666// DESCRIPTION:
667// Gets the where conditions for the SQL in the Advanced Recordset
668//
669// ARGUMENTS:
670// none
671//
672// RETURNS:
673// String with the conditions or false if none defined
674//--------------------------------------------------------------------
675
676function getWhereCondition(){
677 if (isset($this->whereCondition))
678 return $this->whereCondition;
679 else
680 return false;
681}
682
683//--------------------------------------------------------------------
684// FUNCTION:
685// getOrderBy
686//
687// DESCRIPTION:
688// Gets the order by statement for the SQL in the Advanced Recordset
689//
690// ARGUMENTS:
691// none
692//
693// RETURNS:
694// String with the order by statement or false if not defined
695//--------------------------------------------------------------------
696
697function getOrderBy(){
698 if (isset($this->orderBy))
699 return $this->orderBy;
700 else
701 return false;
702}
703
704//--------------------------------------------------------------------
705// FUNCTION:
706// formatDescription
707//
708// DESCRIPTION:
709// Formats the description of each result for hilighting the found words
710//
711// ARGUMENTS:
712// text - the description text
713//
714// RETURNS:
715// the formatted text
716//--------------------------------------------------------------------
717
718function cleanText($text)
719{
720 $text = stripslashes(str_replace(array(" and "," or ","""), " ", strtolower($text)));
721 return $text;
722}
723
724function formatDescription($text){
725 $maxChars = 100;
726 $font = array(0=>"<span class="highlight">",1=>"</span>");
727 $searchFor = $this->searchFor;
728 if(!trim($text)) //empty result
729 return "";
730 $ltext = strtolower($text);
731 if ($this->databaseType=='MySQL' && $this->searchType=='fulltext') {
732 $searchFor = $this->cleanText($searchFor);
733 }
734 $expr = "/"([^"]*)"/m";
735 $matches = array();
736 preg_match_all($expr, $searchFor, $matches);
737 $searchFor = preg_replace($expr,"",$searchFor);
738 $searchFor = $this->cleanText($searchFor);
739 $arrSearchFor = explode(" ",$searchFor);
740 if (is_array($matches[1])) {
741 foreach($matches[1] as $key=>$value) {
742 array_push($arrSearchFor, $value);
743 }
744 }
745 if (strlen($text) > 100) {
746 foreach ($arrSearchFor as $key=>$value) {
747 if ($value!='') {
748 $pos = strpos($ltext, $value);
749 if ($pos) break;
750 }
751 }
752 $leftLength = $pos;
753 $rightLength = strlen(substr($ltext, $pos));
754 if ($leftLength < 50) {
755 $start = 0;
756 $startStr = "";
757 $endStr = "...";
758 } else if ($rightLength < 50) {
759 $start = $pos - (100 - $rightLength);
760 $startStr = "...";
761 $endStr = "";
762 } else {
763 $start = $pos - 50;
764 $startStr = "...";
765 $endStr = "...";
766 }
767 $firstSpace = 0;
768 $lastSpace = strlen($text);
769 if ($start!=0) {
770 $firstSpace = strrpos(substr($text, 0, $start), " ");
771 }
772 if ($start + 100 <strlen($text)) {
773 $lastSpace = strpos($text, " ", $start + 100);
774 }
775 $text = $startStr.substr($text, $firstSpace, $lastSpace - $firstSpace).$endStr;
776 }
777
778 $ltext = strtolower($text);
779 $indexArr = array();
780 for ($i=0;$i<count($arrSearchFor);$i++) {
781 if ($arrSearchFor[$i]!='') {
782 $offset = 0;
783 for ($j=0;$j<substr_count($ltext, $arrSearchFor[$i]);$j++) {
784 $offset = strpos($ltext, $arrSearchFor[$i], $offset);
785 $indexArr[] = array($offset, 0, $i);
786 $offset += strlen($arrSearchFor[$i]);
787 $indexArr[] = array($offset, 1);
788 }
789 }
790 }
791 $ready=false;
792 while(!$ready) {
793 $ready = true;
794 for($i=0;$i<count($indexArr)-1;$i++) {
795 if ($indexArr[$i][0]>$indexArr[$i+1][0]) {
796 $aux = $indexArr[$i];
797 $indexArr[$i] = $indexArr[$i+1];
798 $indexArr[$i+1] = $aux;
799 $ready = false;
800 }
801 }
802 }
803 $displayText = "";
804 $end = 0;
805 for ($i=0; $i<count($indexArr); $i++) {
806 if ($i!=0) {
807 $start = $indexArr[$i-1][0];
808 } else {
809 $start = 0;
810 }
811 $end = $indexArr[$i][0];
812 $type = $indexArr[$i][1];
813 $displayText .= substr($text, $start, $end-$start).$font[$type];
814 }
815 $displayText .= substr($text, $end);
816 return $displayText;
817}