Limit string with/without relating words and from start/end


/ Published in: PHP
Save to your folder(s)

This function allows you to limit the amount of a string, like a sample from a post.
Create a file like function_lmt_txt.php and include it to use it.
Any comments to improve this are welcomed.


Copy this code and paste it in your HTML
  1. /*
  2. Description: Function to limit the text with or without relating words and from the start or end of the string
  3. Usage:
  4. $string = txtlmt($string, "30", "...", "1");
  5.  
  6. Variables:
  7. $txt = string to delimited
  8. $txt_limiter = max amount of letters in the string that should be seen
  9. $txt_sep = the separator used to end the string (like string...)
  10. $tipo
  11. 1 - from start to end without relating words (when it reaches the $txt_limiter it breaks the string)
  12. 2 - from start to end relating words (when it reaches the $txt_limiter it finds the next space to break the string)
  13. 3 - from end to start without relating words (when it reaches the $txt_limiter it breaks the string)
  14. 4 - from edn to start relating words (when it reaches the $txt_limiter it finds the next space to break the string)
  15. */
  16.  
  17. function txtlmt ($txt,$txt_limiter,$txt_sep,$tipo=0) {
  18.  
  19. if($tipo=="" OR $tipo==NULL OR $tipo==0) { $tipo = "2"; }
  20.  
  21. if (strlen($txt) > $txt_limiter) {
  22. // From start to end
  23. if($tipo=="1") { // Do not related words
  24. $txt = substr(put_accents($txt), 0, $txt_limiter) . $txt_sep;
  25. } elseif($tipo=="2") { // Relate words
  26. $txt = substr(put_accents($txt), 0, $txt_limiter);
  27. $txt = substr($txt, 0, strrpos($txt, " ")) . $txt_sep;
  28.  
  29. // From end to start
  30. } elseif($tipo=="3") { // Do not related words
  31. $txt = $txt_sep.substr(put_accents($txt), -$txt_limiter);
  32. } elseif($tipo=="4") { // Relate words
  33. $txt = substr(put_accents($txt), -$txt_limiter);
  34. $txt = $txt_sep.substr($txt, strpos($txt, " ")+1);
  35. }
  36.  
  37. $txt = strip_accents($txt);
  38. return $txt;
  39.  
  40. } else { return $txt; }
  41.  
  42. }
  43.  
  44. // #################################################################
  45.  
  46.  
  47.  
  48. // Function to convert strings to HTML characters
  49. function strip_accents ($string) {
  50. $string = ereg_replace("(á)","á",$string);
  51. $string = ereg_replace("(à)","à",$string);
  52. $string = ereg_replace("(ã)","ã",$string);
  53. $string = ereg_replace("(ó)","ó",$string);
  54. $string = ereg_replace("(õ)","õ",$string);
  55. $string = ereg_replace("(é)","é",$string);
  56. $string = ereg_replace("(ú)","ú",$string);
  57. $string = ereg_replace("(í)","í",$string);
  58. $string = ereg_replace("(ç)","ç",$string);
  59. $string = ereg_replace("(Á)","Á",$string);
  60. $string = ereg_replace("(À)","À",$string);
  61. $string = ereg_replace("(Ã)","Ã",$string);
  62. $string = ereg_replace("(Ó)","Ó",$string);
  63. $string = ereg_replace("(Õ)","Õ",$string);
  64. $string = ereg_replace("(É)","É",$string);
  65. $string = ereg_replace("(Ú)","Ú",$string);
  66. $string = ereg_replace("(Í)","Í",$string);
  67. $string = ereg_replace("(Ç)","Ç",$string);
  68. $string = ereg_replace("(\")",""",$string);
  69. $string = ereg_replace("(<)","<",$string);
  70. $string = ereg_replace("(>)",">",$string);
  71. $string = ereg_replace("(`)","'",$string);
  72. $string = ereg_replace("(')","'",$string);
  73. $string = ereg_replace("º","º",$string);
  74. $string = ereg_replace("ª","ª",$string);
  75. return $string;
  76. }
  77.  
  78. // #################################################################
  79.  
  80.  
  81.  
  82. // Function to convert strings back to HTML characters
  83. function put_accents ($string) {
  84. $string = ereg_replace("(á)","á",$string);
  85. $string = ereg_replace("(à)","à",$string);
  86. $string = ereg_replace("(ã)","ã",$string);
  87. $string = ereg_replace("(ó)","ó",$string);
  88. $string = ereg_replace("(õ)","õ",$string);
  89. $string = ereg_replace("(é)","é",$string);
  90. $string = ereg_replace("(ú)","ú",$string);
  91. $string = ereg_replace("(í)","í",$string);
  92. $string = ereg_replace("(ç)","ç",$string);
  93. $string = ereg_replace("(Á)","Á",$string);
  94. $string = ereg_replace("(À)","À",$string);
  95. $string = ereg_replace("(Ã)","Ã",$string);
  96. $string = ereg_replace("(Ó)","Ó",$string);
  97. $string = ereg_replace("(Õ)","Õ",$string);
  98. $string = ereg_replace("(É)","É",$string);
  99. $string = ereg_replace("(Ú)","Ú",$string);
  100. $string = ereg_replace("(Í)","Í",$string);
  101. $string = ereg_replace("(Ç)","Ç",$string);
  102. $string = ereg_replace("(")","\"",$string);
  103. $string = ereg_replace("(<)","<",$string);
  104. $string = ereg_replace("(>)",">",$string);
  105. $string = ereg_replace("(')","'",$string);
  106. $string = ereg_replace("º","º",$string);
  107. $string = ereg_replace("ª","ª",$string);
  108. return $string;
  109. }
  110.  
  111. // #################################################################

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.