/system/application/helpers/mka_helper.php

https://github.com/mkhairul/Presta · PHP · 147 lines · 94 code · 18 blank · 35 comment · 24 complexity · d6ee584e0e3b1dfe0d5829623c1cb836 MD5 · raw file

  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Code Igniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package
  8. * @author Mohd Khairul
  9. * @copyright
  10. * @license
  11. * @link
  12. * @since Version 1.5.4
  13. * @filesource
  14. */
  15. function error_msg(&$msg, $style=''){
  16. if(getstr($msg))
  17. {
  18. $html = '
  19. <div class="error" '.$style.'>'.$msg.'</div>
  20. ';
  21. }else{ $html = ''; }
  22. return $html;
  23. }
  24. function success_msg(&$msg){
  25. if($msg)
  26. {
  27. $html = '
  28. <div class="success">'.$msg.'</div>
  29. ';
  30. }else{ $html = ''; }
  31. return $html;
  32. }
  33. function getstr(&$tmp){
  34. return (isset($tmp)) ? $tmp : '';
  35. }
  36. function truncate($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) {
  37. if ($considerHtml) {
  38. // if the plain text is shorter than the maximum length, return the whole text
  39. if (strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
  40. return $text;
  41. }
  42. // splits all html-tags to scanable lines
  43. preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER);
  44. $total_length = strlen($ending);
  45. $open_tags = array();
  46. $truncate = '';
  47. foreach ($lines as $line_matchings) {
  48. // if there is any html-tag in this line, handle it and add it (uncounted) to the output
  49. if (!empty($line_matchings[1])) {
  50. // if it's an "empty element" with or without xhtml-conform closing slash (f.e. <br/>)
  51. if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) {
  52. // do nothing
  53. // if tag is a closing tag (f.e. </b>)
  54. } else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) {
  55. // delete tag from $open_tags list
  56. $pos = array_search($tag_matchings[1], $open_tags);
  57. if ($pos !== false) {
  58. unset($open_tags[$pos]);
  59. }
  60. // if tag is an opening tag (f.e. <b>)
  61. } else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) {
  62. // add tag to the beginning of $open_tags list
  63. array_unshift($open_tags, strtolower($tag_matchings[1]));
  64. }
  65. // add html-tag to $truncate'd text
  66. $truncate .= $line_matchings[1];
  67. }
  68. // calculate the length of the plain text part of the line; handle entities as one character
  69. $content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $line_matchings[2]));
  70. if ($total_length+$content_length > $length) {
  71. // the number of characters which are left
  72. $left = $length - $total_length;
  73. $entities_length = 0;
  74. // search for html entities
  75. if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) {
  76. // calculate the real length of all entities in the legal range
  77. foreach ($entities[0] as $entity) {
  78. if ($entity[1]+1-$entities_length <= $left) {
  79. $left--;
  80. $entities_length += strlen($entity[0]);
  81. } else {
  82. // no more characters left
  83. break;
  84. }
  85. }
  86. }
  87. $truncate .= substr($line_matchings[2], 0, $left+$entities_length);
  88. // maximum lenght is reached, so get off the loop
  89. break;
  90. } else {
  91. $truncate .= $line_matchings[2];
  92. $total_length += $content_length;
  93. }
  94. // if the maximum length is reached, get off the loop
  95. if($total_length >= $length) {
  96. break;
  97. }
  98. }
  99. } else {
  100. if (strlen($text) <= $length) {
  101. return $text;
  102. } else {
  103. $truncate = substr($text, 0, $length - strlen($ending));
  104. }
  105. }
  106. // if the words shouldn't be cut in the middle...
  107. if (!$exact) {
  108. // ...search the last occurance of a space...
  109. $spacepos = strrpos($truncate, ' ');
  110. if (isset($spacepos)) {
  111. // ...and cut the text in this position
  112. $truncate = substr($truncate, 0, $spacepos);
  113. }
  114. }
  115. // add the defined ending to the text
  116. $truncate .= $ending;
  117. if($considerHtml) {
  118. // close all unclosed html-tags
  119. foreach ($open_tags as $tag) {
  120. $truncate .= '</' . $tag . '>';
  121. }
  122. }
  123. return $truncate;
  124. }
  125. function required()
  126. {
  127. return '<span class="required">*</span>';
  128. }
  129. ?>