source-function-F_substrHTML

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
Overview

Classes

Interfaces

Exceptions

Functions

  1: <?php
  2: //============================================================+
  3: // File name   : tce_functions_tcecode.php
  4: // Begin       : 2002-01-09
  5: // Last Update : 2013-12-24
  6: //
  7: // Description : Functions to translate TCExam code into XHTML.
  8: //               The TCExam code is compatible to the common BBCode.
  9: //               Supports LaTeX and MathML.
 10: //
 11: // Author: Nicola Asuni
 12: //
 13: // (c) Copyright:
 14: //               Nicola Asuni
 15: //               Tecnick.com LTD
 16: //               www.tecnick.com
 17: //               info@tecnick.com
 18: //
 19: // License:
 20: //    Copyright (C) 2004-2013 Nicola Asuni - Tecnick.com LTD
 21: //    See LICENSE.TXT file for more information.
 22: //============================================================+
 23: 
 24: /**
 25:  * @file
 26:  * Functions to translate TCExam proprietary code into XHTML.
 27:  * The TCExam code is compatible to the common BBCode.
 28:  * @package com.tecnick.tcexam.shared
 29:  * @author Nicola Asuni
 30:  * @since 2002-01-09
 31:  */
 32: 
 33: /**
 34:  * Returns XHTML code from text marked-up with TCExam Code Tags
 35:  * @param $text_to_decode (string) text to convert
 36:  * @return string XHTML code
 37:  */
 38: function F_decode_tcecode($text_to_decode)
 39: {
 40:     require_once('../config/tce_config.php');
 41:     global $l, $db;
 42: 
 43:     // Patterns and replacements
 44:     $pattern = array();
 45:     $replacement = array();
 46:     $i=0;
 47: 
 48:     // escape some special HTML characters
 49:     $newtext = htmlspecialchars($text_to_decode, ENT_NOQUOTES, $l['a_meta_charset']);
 50: 
 51:     // --- convert some BBCode to TCECode: ---
 52:     // [*]list item - convert to new [li] tag
 53:     $newtext = preg_replace("'\[\*\](.*?)\n'i", "[li]\\1[/li]", $newtext);
 54:     // [img]image[/img] - convert to new object tag
 55:     $newtext = preg_replace("'\[img\](.*?)\[/img\]'si", "[object]\\1[/object]", $newtext);
 56:     // [img=WIDTHxHEIGHT]image[/img] - convert to new object tag
 57:     $newtext = preg_replace("'\[img=(.*?)x(.*?)\](.*?)\[/img\]'si", "[object]\\3[/object:\\1:\\2]", $newtext);
 58:     // ---
 59: 
 60:     // [tex]LaTeX_code[/tex]
 61:     $newtext = preg_replace_callback("#\[tex\](.*?)\[/tex\]#si", 'F_latex_callback', $newtext);
 62: 
 63:     // [mathml]MathML_code[/mathml]
 64:     $newtext = preg_replace_callback("#\[mathml\](.*?)\[/mathml\]#si", 'F_mathml_callback', $newtext);
 65: 
 66:     // [object]object_url[/object:width:height:alt]
 67:     $newtext = preg_replace_callback("#\[object\](.*?)\.(.*?)\[/object\:(.*?)\:(.*?)\:(.*?)\]#si", 'F_objects_callback', $newtext);
 68:     // [object]object_url[/object:width:height]
 69:     $newtext = preg_replace_callback("#\[object\](.*?)\.(.*?)\[/object\:(.*?)\:(.*?)\]#si", 'F_objects_callback', $newtext);
 70:     // [object]object_url[/object]
 71:     $newtext = preg_replace_callback("#\[object\](.*?)\.(.*?)\[/object\]#si", 'F_objects_callback', $newtext);
 72: 
 73:     // replace newline chars on [code] tag
 74:     //$newtext = preg_replace("'\r\n'si", "\n",  $newtext);
 75:     //$newtext = preg_replace("'\n\r'si", "\n",  $newtext);
 76:     while (preg_match("'\[code\](.*?) (.*?)\[/code\]'si", $newtext)) {
 77:         $newtext = preg_replace("'\[code\](.*?) (.*?)\[/code\]'si", "[code]\\1&nbsp;\\2[/code]", $newtext);
 78:     }
 79:     /*
 80:     while (preg_match("'\[code\](.*?)\n(.*?)\[/code\]'si", $newtext)) {
 81:         $newtext = preg_replace("'\[code\](.*?)\n(.*?)\[/code\]'si", "[code]\\1@n@\\2[/code]",  $newtext);
 82:     }*/
 83: 
 84:     // [url]http://www.domain.com[/url]
 85:     $pattern[++$i] = "#\[url\](.*?)\[/url\]#si";
 86:     $replacement[++$i] = '<a class="tcecode" href="\1">\1</a>';
 87: 
 88:     // [url=http://www.domain.com]linkname[/url]
 89:     $pattern[++$i] = "#\[url=(.*?)\](.*?)\[/url\]#si";
 90:     $replacement[++$i] = '<a class="tcecode" href="\1">\2</a>';
 91: 
 92:     // [dir=ltr]text direction: ltr, rtl[/dir]
 93:     $pattern[++$i] = "#\[dir=(.*?)\](.*?)\[/dir\]#si";
 94:     $replacement[++$i] = '<span dir="\1">\2</span>';
 95: 
 96:     // [align=left]text alignment: left, right, center, justify[/align]
 97:     $pattern[++$i] = "#\[align=(.*?)\](.*?)\[/align\]#si";
 98:     $replacement[++$i] = '<span style="text-align:\1;">\2</span>';
 99: 
100:     // [code] and [/code] display text as source code
101:     $pattern[++$i] = "#\[code\](.*?)\[/code\]#si";
102:     $replacement[++$i] = '<div class="tcecodepre">\1</div>';
103: 
104:     // [small] and [/small] for small text
105:     $pattern[++$i] = "#\[small\](.*?)\[/small\]#si";
106:     $replacement[++$i] = '<small class="tcecode">\1</small>';
107: 
108:     // [b] and [/b] for bolding text.
109:     $pattern[++$i] = "#\[b\](.*?)\[/b\]#si";
110:     $replacement[++$i] = '<strong class="tcecode">\1</strong>';
111: 
112:     // [i] and [/i] for italicizing text.
113:     $pattern[++$i] = "#\[i\](.*?)\[/i\]#si";
114:     $replacement[++$i] = '<em class="tcecode">\1</em>';
115: 
116:     // [s] and [/s] for strikethrough text.
117:     $pattern[++$i] = "#\[s\](.*?)\[/s\]#si";
118:     $replacement[++$i] = '<span style="text-decoration:line-through;">\1</span>';
119: 
120:     // [u] and [/u] for underlined text.
121:     $pattern[++$i] = "#\[u\](.*?)\[/u\]#si";
122:     $replacement[++$i] = '<span style="text-decoration:underline;">\1</span>';
123: 
124:     // [o] and [/o] for overlined text.
125:     $pattern[++$i] = "#\[o\](.*?)\[/o\]#si";
126:     $replacement[++$i] = '<span style="text-decoration:overline;">\1</span>';
127: 
128:     // [sub] and [/sub] for subscript text.
129:     $pattern[++$i] = "#\[sub\](.*?)\[/sub\]#si";
130:     $replacement[++$i] = '<sub class="tcecode">\1</sub>';
131: 
132:     // [sup] and [/sup] for superscript text.
133:     $pattern[++$i] = "#\[sup\](.*?)\[/sup\]#si";
134:     $replacement[++$i] = '<sup class="tcecode">\1</sup>';
135: 
136:     // [ulist] and [/ulist] unordered list
137:     $pattern[++$i] = "#\[ulist\](.*?)\[/ulist\]#si";
138:     $replacement[++$i] = '<ul class="tcecode">\1</ul>';
139: 
140:     // [olist] and [/olist] ordered list.
141:     $pattern[++$i] = "#\[olist\](.*?)\[/olist\]#si";
142:     $replacement[++$i] = '<ol class="tcecode">\1</ol>';
143: 
144:     // [olist=1] and [/olist] ordered list.
145:     $pattern[++$i] = "#\[olist=1\](.*?)\[/olist\]#si";
146:     $replacement[++$i] = '<ol class="tcecode" style="list-style-type:arabic-numbers">\1</ol>';
147: 
148:     // [olist=a] and [/olist] ordered list.
149:     $pattern[++$i] = "#\[olist=a\](.*?)\[/olist\]#si";
150:     $replacement[++$i] = '<ol class="tcecode" style="list-style-type:lower-alpha">\1</ol>';
151: 
152:     // [li] list items [/li]
153:     $pattern[++$i] = "#\[li\](.*?)\[/li\]#si";
154:     $replacement[++$i] = '<li class="tcecode">\1</li>';
155: 
156:     // [color=#RRGGBB] and [/color]
157:     // [color=rgb(red,green,blue)] and [/color]
158:     // [color=html_color_name] and [/color]
159:     $pattern[++$i] = "#\[color=(.*?)\](.*?)\[/color\]#si";
160:     $replacement[++$i] = '<span style="color:\1">\2</span>';
161: 
162:     // [bgcolor=#RRGGBB] and [/bgcolor]
163:     // [bgcolor=rgb(red,green,blue)] and [/bgcolor]
164:     // [bgcolor=html_color_name] and [/bgcolor]
165:     $pattern[++$i] = "#\[bgcolor=(.*?)\](.*?)\[/bgcolor\]#si";
166:     $replacement[++$i] = '<span style="background-color:\1">\2</span>';
167: 
168:     // [font=value] and [/font]
169:     $pattern[++$i] = "#\[font=(.*?)\](.*?)\[/font\]#si";
170:     $replacement[++$i] = '<span style="font-family:\1">\2</span>';
171: 
172:     // [size=value] and [/size]
173:     // [size=+value] and [/size]
174:     // [size=value%] and [/size]
175:     $pattern[++$i] = "#\[size=([+\-]?[0-9a-z\-]+[%]?)\](.*?)\[/size\]#si";
176:     $replacement[++$i] = '<span style="font-size:\1">\2</span>';
177: 
178:     $newtext = preg_replace($pattern, $replacement, $newtext);
179: 
180:     // line breaks
181:     $newtext = preg_replace("'(\r\n|\n|\r)'", '<br />', $newtext);
182:     $newtext = str_replace('<br /><li', '<li', $newtext);
183:     $newtext = str_replace('</li><br />', '</li>', $newtext);
184:     $newtext = str_replace('<br /><param', '<param', $newtext);
185: 
186:     // restore newline chars on [code] tag
187:     //$newtext = preg_replace("'@n@'si", "\n",  $newtext);
188: 
189:     return ($newtext);
190: }
191: 
192: /**
193:  * Callback function for preg_replace_callback (LaTeX replacement).
194:  * Returns replacement image for LaTeX code.
195:  * @param $matches (string) array containing matches: $matches[0] is the complete match, $matches[1] the match for the first subpattern enclosed in '(...)' (the LaTeX code)
196:  * @return string replacement HTML code string to include the equivalent LaTeX image.
197:  */
198: function F_latex_callback($matches)
199: {
200:     require_once('../../shared/config/tce_latex.php');
201:     // extract latex code and convert some entities
202:     $latex = unhtmlentities($matches[1]);
203:     $latex = str_replace("&gt;", '>', $latex);
204:     $latex = str_replace("&lt;", '<', $latex);
205:     $dr = 3; // density ratio
206:     // generate file name
207:     $filename = K_LATEX_IMG_PREFIX.md5($latex);
208:     $imgpath = K_LATEX_PATH_PICTURE.$filename;
209:     $imgurl = false;
210:     $error = '';
211:     // check if file is already cached
212:     if (is_file($imgpath.'.'.K_LATEX_IMG_FORMAT)) {
213:         $imgurl = K_LATEX_PATH_PICTURE_HTTPD.$filename.'.'.K_LATEX_IMG_FORMAT;
214:     } else {
215:         // check if the formula
216:         if (strlen($latex) > K_LATEX_MAX_LENGHT) {
217:             $error = 'the formula is too long';
218:         } elseif (preg_match('/(include|def|command|loop|repeat|open|toks|output|input|catcode|name|[\^]{2}|\\\\every|\\\\errhelp|\\\\errorstopmode|\\\\scrollmode|\\\\nonstopmode|\\\\batchmode|\\\\read|\\\\write|csname|\\\\newhelp|\\\\uppercase|\\\\lowercase|\\\\relax|\\\\aftergroup|\\\\afterassignment|\\\\expandafter|\\\\noexpand|\\\\special)/i', $latex) > 0) {
219:             $error = 'invalid command';
220:         } else {
221:             // wrap the formula
222:             $ltx = '\nonstopmode'."\n";
223:             $ltx .= '\documentclass{'.K_LATEX_CLASS.'}'."\n";
224:             $ltx .= '\usepackage[T1]{fontenc}'."\n";
225:             $ltx .= '\usepackage{amsmath,amsfonts,amssymb,wasysym,latexsym,marvosym,txfonts}'."\n";
226:             $ltx .= '\usepackage[pdftex]{color}'."\n";
227:             $ltx .= '\pagestyle{empty}'."\n";
228:             $ltx .= '\begin{document}'."\n";
229:             $ltx .= '\fontsize{'.K_LATEX_FONT_SIZE.'}{'.(2 * K_LATEX_FONT_SIZE).'}'."\n";
230:             $ltx .= '\selectfont'."\n";
231:             $ltx .= '\color{black}'."\n";
232:             $ltx .= '\pagecolor{white}'."\n";
233:             $ltx .= '$'.$latex.'$'."\n";
234:             $ltx .= '\end{document}'."\n";
235:             if (file_put_contents($imgpath.'.tex', $ltx) === false) {
236:                 $error = 'unable to write on the cache folder';
237:             } else {
238:                 $cmd = 'cd '.K_LATEX_PATH_PICTURE.' && '.K_LATEX_PDFLATEX.' '.$imgpath.'.tex';
239:                 $sts = exec($cmd, $out, $ret);
240:                 if (!$sts) {
241:                     $error = implode("\n", $out);
242:                 } else {
243:                     // convert code using ImageMagick
244:                     $cmd = 'cd '.K_LATEX_PATH_PICTURE.' && '.K_LATEX_PATH_CONVERT.' -density '.(K_LATEX_FORMULA_DENSITY * $dr).' -trim +repage '.$imgpath.'.pdf -depth 8 -quality 100 '.$imgpath.'.'.K_LATEX_IMG_FORMAT;
245:                     unset($out);
246:                     $sts = exec($cmd, $out, $ret);
247:                     if ($ret != 0) {
248:                         $error = implode("\n", $out);
249:                     } else {
250:                         $imsize = @getimagesize($imgpath.'.'.K_LATEX_IMG_FORMAT);
251:                         list($w, $h) = $imsize;
252:                         if ((($w / $dr) > K_LATEX_MAX_WIDTH) or (($h / $dr) > K_LATEX_MAX_HEIGHT)) {
253:                             $error = 'image size exceed limits';
254:                         } else {
255:                             $imgurl = K_LATEX_PATH_PICTURE_HTTPD.$filename.'.'.K_LATEX_IMG_FORMAT;
256:                         }
257:                     }
258:                 }
259:             }
260:             // remove temporary files (if any)
261:             $tmpext = array('tex', 'aux', 'log', 'pdf');
262:             foreach ($tmpext as $ext) {
263:                 if (file_exists($imgpath.'.'.$ext)) {
264:                     @unlink($imgpath.'.'.$ext);
265:                 }
266:             }
267:         }
268:     }
269:     if ($imgurl === false) {
270:         $newtext = '[LaTeX: ERROR '.$error.']';
271:     } else {
272:         // alternative text to image
273:         $alt_latex = '[LaTeX]'."\n".htmlentities($latex, ENT_QUOTES);
274:         $replaceTable = array("\r" => '&#13;', "\n" => '&#10;');
275:         $alt_latex = strtr($alt_latex, $replaceTable);
276:         // XHTML code for image
277:         $imsize = @getimagesize($imgpath.'.'.K_LATEX_IMG_FORMAT);
278:         list($w, $h) = $imsize;
279:         $newtext = '<img src="'.$imgurl.'" alt="'.$alt_latex.'" class="tcecode" width="'.round($w / $dr).'" height="'.round($h / $dr).'" />';
280:     }
281:     return $newtext;
282: }
283: 
284: /**
285:  * Callback function for preg_replace_callback (MathML replacement).
286:  * Returns replacement code for MathML code.
287:  * @param $matches (string) array containing matches: $matches[0] is the complete match, $matches[1] the match for the first subpattern enclosed in '(...)' (the MathML code)
288:  * @return string MathML code.
289:  */
290: function F_mathml_callback($matches)
291: {
292:     $mathml_tags = '<abs><and><annotation><annotation-xml><apply><approx><arccos><arccosh><arccot><arccoth><arccsc><arccsch><arcsec><arcsech><arcsin><arcsinh><arctan><arctanh><arg><bind><bvar><card><cartesianproduct><cbytes><ceiling><cerror><ci><cn><codomain><complexes><compose><condition><conjugate><cos><cosh><cot><coth><cs><csc><csch><csymbol><curl><declare><degree><determinant><diff><divergence><divide><domain><domainofapplication><el><emptyset><eq><equivalent><eulergamma><exists><exp><exponentiale><factorial><factorof><false><floor><fn><forall><gcd><geq><grad><gt><ident><image><imaginary><imaginaryi><implies><in><infinity><int><integers><intersect><interval><inverse><lambda><laplacian><lcm><leq><limit><list><ln><log><logbase><lowlimit><lt><maction><malign><maligngroup><malignmark><malignscope><math><matrix><matrixrow><max><mean><median><menclose><merror><mfenced><mfrac><mfraction><mglyph><mi><min><minus><mlabeledtr><mlongdiv><mmultiscripts><mn><mo><mode><moment><momentabout><mover><mpadded><mphantom><mprescripts><mroot><mrow><ms><mscarries><mscarry><msgroup><msline><mspace><msqrt><msrow><mstack><mstyle><msub><msubsup><msup><mtable><mtd><mtext><mtr><munder><munderover><naturalnumbers><neq><none><not><notanumber><note><notin><notprsubset><notsubset><or><otherwise><outerproduct><partialdiff><pi><piece><piecewise><plus><power><primes><product><prsubset><quotient><rationals><real><reals><reln><rem><root><scalarproduct><sdev><sec><sech><selector><semantics><sep><set><setdiff><share><sin><sinh><subset><sum><tan><tanh><tendsto><times><transpose><true><union><uplimit><variance><vector><vectorproduct><xor>';
293:     // extract latex code and convert some entities
294:     $mathml = unhtmlentities($matches[1]);
295:     $mathml = str_replace("&gt;", '>', $mathml);
296:     $mathml = str_replace("&lt;", '<', $mathml);
297:     // remove all non-MathML tags
298:     $mathml = strip_tags($mathml, $mathml_tags);
299:     $mathml = preg_replace("/[\n\r\s]+/", ' ', $mathml);
300:     $mathml = trim($mathml);
301:     if (strpos($mathml, '<math') !== 0) {
302:         // add default math parent tag
303:         $mathml = '<math xmlns="http://www.w3.org/1998/Math/MathML">'.$mathml.'</math>';
304:     }
305:     return $mathml;
306: }
307: 
308: /**
309:  * Callback function for preg_replace_callback.
310:  * Returns replacement code by MIME type.
311:  * @param $matches (string) array containing matches: $matches[0] is the complete match, $matches[1] the match for the first subpattern enclosed in '(...)' and so on
312:  * @return string replacement string by file extension
313:  */
314: function F_objects_callback($matches)
315: {
316:     $width = 0;
317:     $height = 0;
318:     $alt = '';
319:     if (isset($matches[3]) and ($matches[3] > 0)) {
320:         $width = $matches[3];
321:     }
322:     if (isset($matches[4]) and ($matches[4] > 0)) {
323:         $height = $matches[4];
324:     }
325:     if (isset($matches[5]) and (!empty($matches[5]))) {
326:         $alt = F_tcecodeToTitle($matches[5]);
327:     }
328:     return F_objects_replacement($matches[1], $matches[2], $width, $height, $alt);
329: }
330: 
331: /**
332:  * Returns the xhtml code needed to display the object by MIME type.
333:  * @param $name (string) object path excluded extension
334:  * @param $extension (string) object extension (e.g.: gif, jpg, swf, ...)
335:  * @param $width (int) object width
336:  * @param $height (int) object height
337:  * @param $maxwidth (int) object max or default width
338:  * @param $maxheight (int) object max or default height
339:  * @param $alt (string) alternative content
340:  * @return string replacement string
341:  */
342: function F_objects_replacement($name, $extension, $width = 0, $height = 0, $alt = '', &$maxwidth = 0, &$maxheight = 0)
343: {
344:     require_once('../config/tce_config.php');
345:     global $l, $db;
346:     $filename = $name.'.'.$extension;
347:     $extension = strtolower($extension);
348:     $htmlcode = '';
349:     switch ($extension) {
350:         case 'gif':
351:         case 'jpg':
352:         case 'jpeg':
353:         case 'png':
354:         case 'svg': { // images
355:             $htmlcode = '<img src="'.K_PATH_URL_CACHE.$filename.'"';
356:             if (!empty($alt)) {
357:                 $htmlcode .= ' alt="'.$alt.'"';
358:             } else {
359:                 $htmlcode .= ' alt="image:'.$filename.'"';
360:             }
361:             $imsize = @getimagesize(K_PATH_CACHE.$filename);
362:             if ($imsize !== false) {
363:                 list($pixw, $pixh) = $imsize;
364:                 if (($width <= 0) and ($height <= 0)) {
365:                     // get default size
366:                     $width = $pixw;
367:                     $height = $pixh;
368:                 } elseif ($width <= 0) {
369:                     $width = $height * $pixw / $pixh;
370:                 } elseif ($height <= 0) {
371:                     $height = $width * $pixh / $pixw;
372:                 }
373:             }
374:             $ratio = 1;
375:             if (($width > 0) and ($height > 0)) {
376:                 $ratio = $width / $height;
377:             }
378:             // fit image on max dimensions
379:             if (($maxwidth > 0) and ($width > $maxwidth)) {
380:                 $width = $maxwidth;
381:                 $height = round($width / $ratio);
382:                 $maxheight = min($maxheight, $height);
383:             }
384:             if (($maxheight > 0) and ($height > $maxheight)) {
385:                 $height = $maxheight;
386:                 $width = round($height * $ratio);
387:             }
388:             // print size
389:             if ($width > 0) {
390:                 $htmlcode .= ' width="'.$width.'"';
391:             }
392:             if ($height > 0) {
393:                 $htmlcode .= ' height="'.$height.'"';
394:             }
395:             $htmlcode .= ' class="tcecode" />';
396:             if ($imsize !== false) {
397:                 $maxwidth = $pixw;
398:                 $maxheight = $pixh;
399:             }
400:             break;
401:         }
402:         default: {
403:             include('../../shared/config/tce_mime.php');
404:             if (isset($mime[$extension])) {
405:                 $htmlcode = '<object type="'.$mime[$extension].'" data="'.K_PATH_URL_CACHE.$filename.'"';
406:                 if ($width >0) {
407:                     $htmlcode .= ' width="'.$width.'"';
408:                 } elseif ($maxwidth > 0) {
409:                     $htmlcode .= ' width="'.$maxwidth.'"';
410:                 }
411:                 if ($height >0) {
412:                     $htmlcode .= ' height="'.$height.'"';
413:                 } elseif ($maxheight > 0) {
414:                     $htmlcode .= ' height="'.$maxheight.'"';
415:                 }
416:                 $htmlcode .= '>';
417:                 $htmlcode .= '<param name="type" value="'.$mime[$extension].'" />';
418:                 $htmlcode .= '<param name="src" value="'.K_PATH_URL_CACHE.$filename.'" />';
419:                 $htmlcode .= '<param name="filename" value="'.K_PATH_URL_CACHE.$filename.'" />';
420:                 if ($width > 0) {
421:                     $htmlcode .= '<param name="width" value="'.$width.'" />';
422:                 } elseif ($maxwidth > 0) {
423:                     $htmlcode .= '<param name="width" value="'.$maxwidth.'" />';
424:                 }
425:                 if ($height > 0) {
426:                     $htmlcode .= '<param name="height" value="'.$height.'" />';
427:                 } elseif ($maxheight > 0) {
428:                     $htmlcode .= '<param name="height" value="'.$maxheight.'" />';
429:                 }
430:                 if (!empty($alt)) {
431:                     $htmlcode .= ''.$alt.'';
432:                 } else {
433:                     $htmlcode .= '['.$mime[$extension].']:'.$filename.'';
434:                 }
435:                 $htmlcode .= '</object>';
436:             } else {
437:                 $htmlcode = '[ERROR - UNKNOW MIME TYPE FOR: '.$extension.']';
438:             }
439:             break;
440:         }
441:     }
442:     return $htmlcode;
443: }
444: 
445: /**
446:  * Returns specified string without tcecode mark-up tags
447:  * @param $str (string) text to process
448:  * @return string without tcecode markup tags
449:  */
450: function F_remove_tcecode($str)
451: {
452:     $str = preg_replace("'\[object\](.*?)\[/object([^\]]*?)\]'si", '[OBJ]', $str);
453:     $str = preg_replace("'\[img([^\]]*?)\](.*?)\[/img\]'si", '[IMG]', $str);
454:     $str = preg_replace("'\[code\](.*?)\[/code\]'si", '\1', $str);
455:     $str = preg_replace("'\[b\](.*?)\[/b\]'si", '\1', $str);
456:     $str = preg_replace("'\[i\](.*?)\[/i\]'si", '\1', $str);
457:     $str = preg_replace("'\[s\](.*?)\[/s\]'si", '\1', $str);
458:     $str = preg_replace("'\[u\](.*?)\[/u\]'si", '\1', $str);
459:     $str = preg_replace("'\[o\](.*?)\[/o\]'si", '\1', $str);
460:     $str = preg_replace("'\[color([^\]]*?)\](.*?)\[/color\]'si", '\2', $str);
461:     $str = preg_replace("'\[bgcolor([^\]]*?)\](.*?)\[/bgcolor\]'si", '\2', $str);
462:     $str = preg_replace("'\[font([^\]]*?)\](.*?)\[/font\]'si", '\2', $str);
463:     $str = preg_replace("'\[size([^\]]*?)\](.*?)\[/size\]'si", '\2', $str);
464:     $str = preg_replace("'\[small\](.*?)\[/small\]'si", '\1', $str);
465:     $str = preg_replace("'\[sub\](.*?)\[/sub\]'si", '\1', $str);
466:     $str = preg_replace("'\[sup\](.*?)\[/sup\]'si", '\1', $str);
467:     $str = preg_replace("'\[url([^\]]*?)\](.*?)\[/url\]'si", '\2', $str);
468:     $str = preg_replace("'\[li\](.*?)\[/li\]'si", ' * \1', $str);
469:     $str = preg_replace("'\[\*\](.*?)\n'i", ' * \1', $str);
470:     $str = preg_replace("'\[ulist\](.*?)\[/ulist\]'si", '\1', $str);
471:     $str = preg_replace("'\[olist([^\]]*?)\](.*?)\[/olist\]'si", '\2', $str);
472:     $str = preg_replace("'\[tex\](.*?)\[/tex\]'si", '[TEX]', $str);
473:     return $str;
474: }
475: 
476: /**
477:  * Converts tcecode text to a single XHTML string removing some objects.
478:  * @param $str (string) text to process
479:  * return string
480:  */
481: function F_tcecodeToLine($str)
482: {
483:     $str = preg_replace("'\[object\](.*?)\[/object([^\]]*?)\]'si", '[OBJ]', $str);
484:     $str = preg_replace("'\[img([^\]]*?)\](.*?)\[/img\]'si", '[IMG]', $str);
485:     $str = preg_replace("'\[code\](.*?)\[/code\]'si", '\1', $str);
486:     $str = preg_replace("'\[li\](.*?)\[/li\]'si", ' * \1', $str);
487:     $str = preg_replace("'\[\*\](.*?)\n'i", ' * \1', $str);
488:     $str = preg_replace("'\[ulist\](.*?)\[/ulist\]'si", '\1', $str);
489:     $str = preg_replace("'\[olist([^\]]*?)\](.*?)\[/olist\]'si", '\2', $str);
490:     $str = preg_replace("'\[url([^\]]*?)\](.*?)\[/url\]'si", '\2', $str);
491:     $str = preg_replace("'\[tex\](.*?)\[/tex\]'si", '[TEX]', $str);
492:     $str = F_compact_string($str);
493:     $str = F_decode_tcecode($str);
494:     $str = F_compact_string($str);
495:     if (strlen($str) > K_QUESTION_LINE_MAX_LENGTH) {
496:         $str = F_substrHTML($str, K_QUESTION_LINE_MAX_LENGTH, 20).' ...';
497:     }
498:     return $str;
499: }
500: 
501: /**
502:  * Converts tcecode text to simple string for XHTML title attribute.
503:  * @param $str (string) text to process
504:  * return string
505:  */
506: function F_tcecodeToTitle($str)
507: {
508:     require_once('../config/tce_config.php');
509:     global $l;
510:     $str = F_remove_tcecode($str);
511:     $str = F_compact_string($str);
512:     $str = htmlspecialchars($str, ENT_COMPAT, $l['a_meta_charset']);
513:     return $str;
514: }
515: 
516: /**
517:  * Return a substring of XHTML code while making sure no html tags are chopped.
518:  * It also prevents chopping while a tag is still open.
519:  * this function is based on a public-domain script posted on www.php.net by fox@conskript.server and mr@bbp.biz
520:  * @param $htmltext (string)
521:  * @param $min_length (int) (default=100) the approximate length you want the concatenated text to be
522:  * @param $offset_length (int) (default=20) the max variation in how long the text can be
523:  */
524: function F_substrHTML($htmltext, $min_length = 100, $offset_length = 20)
525: {
526:     // Reset tag counter and quote checker
527:     $tag_counter = 0;
528:     $quotes_on = false;
529:     // Check if the text is too long
530:     if (strlen($htmltext) > $min_length) {
531:         // Reset the tag_counter and pass through (part of) the entire text
532:         $c = 0;
533:         for ($i = 0; $i < strlen($htmltext); $i++) {
534:             // Load the current character and the next one if the string has not arrived at the last character
535:             $current_char = substr($htmltext, $i, 1);
536:             if ($i < strlen($htmltext) - 1) {
537:                 $next_char = substr($htmltext, $i + 1, 1);
538:             } else {
539:                 $next_char = '';
540:             }
541:             // First check if quotes are on
542:             if (!$quotes_on) {
543:                 // Check if it's a tag On a "<" add 3 if it's an opening tag (like <a href...) or add only 1 if it's an ending tag (like </a>)
544:                 if ($current_char == '<') {
545:                     if ($next_char == '/') {
546:                         $tag_counter += 1;
547:                     } else {
548:                         $tag_counter += 3;
549:                     }
550:                 }
551:                 // Slash signifies an ending (like </a> or ... />) substract 2
552:                 if (($current_char == '/') and ($tag_counter != 0)) {
553:                     $tag_counter -= 2;
554:                 }
555:                 // On a ">" substract 1
556:                 if ($current_char == '>') {
557:                     $tag_counter -= 1;
558:                 }
559:                 // If quotes are encountered, start ignoring the tags (for directory slashes)
560:                 if ($current_char == '"') {
561:                     $quotes_on = true;
562:                 }
563:             } else {
564:                 // IF quotes are encountered again, turn it back off
565:                 if ($current_char == '"') {
566:                     $quotes_on = false;
567:                 }
568:             }
569:             // Count only the chars outside html tags
570:             if (($tag_counter == 2) or ($tag_counter == 0)) {
571:                 $c++;
572:             }
573:             // Check if the counter has reached the minimum length yet,
574:             // then wait for the tag_counter to become 0, and chop the string there
575:             if (($c > $min_length - $offset_length) and ($tag_counter == 0) and ($next_char == ' ')) {
576:                 $htmltext = substr($htmltext, 0, $i + 1);
577:                 return $htmltext;
578:             }
579:         }
580:     }
581:     return $htmltext;
582: }
583: 
584: //============================================================+
585: // END OF FILE
586: //============================================================+
587: 
 

© 2004-2018 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy