PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/texts.php

https://bitbucket.org/Balancer/bors-core
PHP | 186 lines | 137 code | 44 blank | 5 comment | 28 complexity | fd953c30efede961ae2a1c4757892673 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. require_once('strings.php');
  3. function strip_text($text, $len=192, $more_text = NULL, $microstyle = false, $wrap = 0)
  4. {
  5. if(is_null($more_text))
  6. $more_text = ec('…');
  7. // $text = to_one_string($text);
  8. while(preg_match("#^(.*)<!\-\-QuoteBegin.*?\-\->.+?<!\-\-QuoteEEnd\-\->#is",$text))
  9. $text=preg_replace("#^(.*)<!\-\-QuoteBegin.*?\-\->.+?<!\-\-QuoteEEnd\-\->#is","$1",$text);
  10. $text=preg_replace("!(^|<p>|<br>|<br />)+(<font[^>]+>)(<p>)?[^\s<]*>.+?(<br />|<br>|<p>|$)+!i","",$text);
  11. $text=preg_replace("!(^|<p>|<br>|<br />)+(<font[^>]+>)(<a [^>]+>)?[^\s<]*</a>>.+?(<br />|<br>|<p>|$)+!i","",$text);
  12. $text=preg_replace("!<font size=\-2 color=#\d\d\d\d\d\d><b>.+?</b>>.+?</font>(<br>)+!is","",$text);
  13. $text=preg_replace("!(<br>|<br />)+!i","<br />",$text);
  14. // $text=preg_replace("!</?(table|tr|td|h\d|br|p)[^>]*>!i"," ",$text);
  15. $text=str_replace("\n"," ",$text);
  16. $text=preg_replace("/\s+/"," ",$text);
  17. $text=preg_replace("/^\s+/","",$text);
  18. $text=preg_replace("/\s+$/","",$text);
  19. $text=preg_replace("/^#nav(.+?)#/","",$text);
  20. $text=preg_replace("!\-+!","-",$text);
  21. if($microstyle)
  22. {
  23. $text = preg_replace('!<br[^>]*>!', "\n", $text);
  24. $text = preg_replace('!^\s*\S+>.*$!m', '', $text);
  25. $text = str_replace("\n", " ", $text);
  26. $text = preg_replace("/\s{2,}/", " ", trim($text));
  27. $text = preg_replace("!\[/?\w+[^\]]*\]!", " ", trim($text));
  28. $text = strip_tags($text);
  29. $text = str_replace(array(':eek:'), array('o_O'), $text);
  30. if(bors_strlen($text) > $len)
  31. {
  32. $text = bors_substr($text, 0, $len-bors_strlen($more_text));
  33. $space_pos = bors_strrpos($text, ' ');
  34. if($space_pos !== false)
  35. $text = bors_substr($text, 0, $space_pos);
  36. $text = bors_close_tags($text);
  37. $text .= $more_text;
  38. }
  39. // if($wrap)
  40. // $text = str_replace('———', '&shy;', htmlspecialchars(blib_string::wordwrap($text, $wrap, '———',true)));
  41. return $text;
  42. }
  43. if(bors_strlen($text) > $len)
  44. {
  45. $res="";
  46. $do_flag=1;
  47. $in_tag=0;
  48. while($do_flag && $text)
  49. {
  50. $c = bors_substr($text,0,1);
  51. $text = bors_substr($text,1);
  52. $res.=$c;
  53. if($c=='<') $in_tag++;
  54. if($c=='>' && $in_tag>0) $in_tag--;
  55. if(!$in_tag && bors_strlen($res)>=$len)
  56. $do_flag=0;
  57. }
  58. $res = bors_close_tags($res);
  59. $text = $res . $more_text;
  60. }
  61. if(config('is_test'))
  62. echo $text, PHP_EOL, PHP_EOL;
  63. return $text;
  64. }
  65. function bors_close_tags($html) { return blib_html::close_tags($html); }
  66. function bors_close_bbtags($text)
  67. {
  68. $close_tags = explode(" ", "a b blockquote dd div dl dt em embed font i iframe object option p param pre s select small span table td tr tt u xmp");
  69. foreach($close_tags as $tag)
  70. {
  71. $n = preg_match_all("!\[$tag(\s|\])!i", $text, $m) - preg_match_all("!\[/$tag(\s|\])!i", $text, $m);
  72. if($n == 0)
  73. continue;
  74. if($n > 0) // Открывающихся больше закрывающихся
  75. {
  76. $text .= str_repeat("[/$tag]", $n);
  77. continue;
  78. }
  79. // Закрывающихся больше открывающихся
  80. $text = str_repeat("[$tag]", -$n) . $text;
  81. }
  82. return $text;
  83. }
  84. function to_one_string($s)
  85. {
  86. if(sizeof($s)>1)
  87. $s=join("\n",$s);
  88. $s=str_replace("\n","<br />",$s);
  89. $s=str_replace("\|","&#124;",$s);
  90. $s=str_replace("\r","",$s);
  91. return $s;
  92. }
  93. function quote_fix($text)
  94. {
  95. if(!empty($GLOBALS['bors_data']['config']['gpc']) && preg_match("!\\\\!", $text))
  96. return stripslashes($text);
  97. return $text;
  98. }
  99. function bors_text_clear($text, $morfology = true, $spacer = ' ', $lowercase = true)
  100. {
  101. if($morfology)
  102. require_once('classes/inc/text/Stem_ru.php');
  103. $text = str_replace(
  104. ['«','»','№', '>'],
  105. [' ',' ',' ', '> '],
  106. $text);
  107. $text = strip_tags($text);
  108. $text = preg_replace(
  109. ['/&\w+;/', '/&#\d+;/', "![\x01-, :-@ [-` {-~]!x", '/[^\w\.\-]/u'],
  110. [' ', ' ', ' ', ' '],
  111. $text);
  112. $result = trim(preg_replace('/\s\s+/', ' ', $text));
  113. if($lowercase)
  114. $result = bors_lower($result);
  115. if($morfology)
  116. {
  117. static $Stemmer = false;
  118. static $cache = array();
  119. if(!$Stemmer)
  120. $Stemmer = new Lingua_Stem_Ru();
  121. $words = array();
  122. foreach(explode(' ', $result) as $word)
  123. if(array_key_exists($word, $cache))
  124. $words[] = $cache[$word];
  125. else
  126. $words[] = $cache[$word] = $Stemmer->stem_word($word);
  127. $result = join(' ', $words);
  128. }
  129. $result = ' '.$result.' ';
  130. if($spacer != ' ')
  131. $result = str_replace(' ', $spacer, $result);
  132. return $result;
  133. }
  134. function clause_truncate_ceil($text, $limit, $max_limit = NULL)
  135. {
  136. if(is_null($max_limit))
  137. $max_limit = $limit * 2;
  138. if(preg_match("/^(.{".$limit."}.+?[\.\?!])(\n|\s|$)/su", $text, $m))
  139. if(bors_strlen($m[1]) <= $max_limit)
  140. return $m[1];
  141. return truncate($text, $max_limit, ec('…'));
  142. }