PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/assets/ddeb172d/tests/typo.php

https://bitbucket.org/graaaf/erso
PHP | 98 lines | 63 code | 24 blank | 11 comment | 8 complexity | dd9ad5b02dec0b6e977df213349e22b9 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, LGPL-2.1, BSD-3-Clause, BSD-2-Clause
  1. <?php
  2. $html = stripslashes(urldecode($_POST['editor']));
  3. echo typo($html);
  4. function typo($html, $lang = 'ru')
  5. {
  6. $html = stripslashes($html);
  7. // remove pre
  8. preg_match_all('/<pre>([\\w\\W]*?)<\/pre>/i', $html, $matches);
  9. foreach ($matches[0] as $k => $v)
  10. {
  11. $html = str_replace($v, md5($v), $html);
  12. $pre_cache[md5($v)] = $v;
  13. }
  14. // remove style
  15. preg_match_all('/<style([\\w\\W]*?)<\/style>/i', $html, $matches);
  16. foreach ($matches[0] as $k => $v)
  17. {
  18. $html = str_replace($v, md5($v), $html);
  19. $pre_css[md5($v)] = $v;
  20. }
  21. // remove script
  22. preg_match_all('/<script([\\w\\W]*?)<\/script>/i', $html, $matches);
  23. foreach ($matches[0] as $k => $v)
  24. {
  25. $html = str_replace($v, md5($v), $html);
  26. $pre_script[md5($v)] = $v;
  27. }
  28. // remove tags
  29. preg_match_all('/<(.*?)>/i', $html, $tag_cache);
  30. foreach ($tag_cache[1] as $k => $v)
  31. {
  32. $html = str_replace($tag_cache[0][$k], '<'.md5($v).'>', $html);
  33. $full_cache['<'.md5($v).'>'] = $tag_cache[0][$k];
  34. }
  35. // Blank
  36. $html = preg_replace('/(\s)+/i', "$1", $html);
  37. $html = preg_replace('/(\n?\A)\s+(?!\-)/i', "$1", $html);
  38. $html = preg_replace('/\s\z/i', "", $html);
  39. // Mdash
  40. $html = preg_replace('/(>|\A|\n)\-\s/i', "$1&mdash;&nbsp;", $html);
  41. // One-two words
  42. $html = preg_replace('/(?<![-:])\b([\w]{1,2}\b(?:[,:;]?))(?!\n)\s/i', "$1&nbsp;", $html);
  43. if ($lang == 'ru')
  44. {
  45. $html = preg_replace('/(\s|&nbsp;)(же|ли|ль|бы|б|ж|ка)([\.,!\?:;])?&nbsp;/i', "&nbsp;$2$3 ", $html);
  46. }
  47. // Replace special characters
  48. $html = preg_replace('/·/i', "&bull;", $html);
  49. $html = preg_replace('/•/i', "&bull;", $html);
  50. $html = preg_replace('/«/i', "&laquo;", $html);
  51. $html = preg_replace('/»/i', "&raquo;", $html);
  52. $html = preg_replace('/\.{3}/i', "&hellip;", $html);
  53. $html = preg_replace('/\((c|с)\)/i', "&copy;", $html);
  54. $html = preg_replace('/\(r\)/i', '<sup><small>&reg;</small></sup>', $html);
  55. $html = preg_replace('/\(tm\)/i', '<sup><small>&trade;</small></sup>', $html);
  56. $html = preg_replace('/(\d+)(x|х)(\d+)/i', '$1&times;$3', $html);
  57. $html = preg_replace('/(\+\-|\-\+|\+\-)/i', '&plusmn;', $html);
  58. $html = preg_replace('/([ ]+|&nbsp;)\-\s+/i', '&nbsp;&mdash; ', $html);
  59. // Russian quotes
  60. if ($lang == 'ru')
  61. {
  62. $html = preg_replace('/(?<!\s)([!?]|&hellip;)?"(?!\b)/i', '$1&raquo;', $html);
  63. $html = preg_replace('/(?<!\b)"(?!\s)/i', '&laquo;', $html);
  64. }
  65. // Dash
  66. $html = preg_replace('/(?<!\-)(?=\b)(\w+)\-(\w+)(?<=\b)(?!\-)/i', '<span style="white-space: none;">$1-$2</span>', $html);
  67. if (isset($pre_cache)) foreach($pre_cache as $k => $v) $html = str_replace($k, $v, $html);
  68. if (isset($pre_css)) foreach($pre_css as $k => $v) $html = str_replace($k, $v, $html);
  69. if (isset($pre_script)) foreach($pre_script as $k => $v) $html = str_replace($k, $v, $html);
  70. // return tags
  71. if (isset($full_cache)) foreach($full_cache as $k => $v) $html = str_replace($k, $v, $html);
  72. return $html;
  73. }
  74. ?>