Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. $big = "This is a sentence that has more than 100 characters in it, and I want to return a string of only full words that is no more than 100 characters!"
  2.  
  3. $small = some_function($big);
  4.  
  5. $small = "This is a sentence that has more than 100 characters in it, and I want to return a string of only"
  6.  
  7. $pos=strpos($content, ' ', 200);
  8. substr($content,0,$pos );
  9.  
  10. //truncate a string only at a whitespace (by nogdog)
  11. function truncate($text, $length) {
  12. $length = abs((int)$length);
  13. if(strlen($text) > $length) {
  14. $text = preg_replace("/^(.{1,$length})(s.*|$)/s", '\1...', $text);
  15. }
  16. return($text);
  17. }
  18.  
  19. function limitString($string, $limit = 100) {
  20. // Return early if the string is already shorter than the limit
  21. if(strlen($string) < $limit) {return $string;}
  22.  
  23. $regex = "/(.{1,$limit})b/";
  24. preg_match($regex, $string, $matches);
  25. return $matches[1];
  26. }
  27.  
  28. function truncate($str, $len) {
  29. $tail = max(0, $len-10);
  30. $trunk = substr($str, 0, $tail);
  31. $trunk .= strrev(preg_replace('~^..+?[s,:]b|^...~', '...', strrev(substr($str, $tail, $len-$tail))));
  32. return $trunk;
  33. }
  34.  
  35. <?PHP
  36. $big = "This is a sentence that has more than 100 characters in it, and I want to return a string of only full words that is no more than 100 characters!";
  37. $small = some_function($big);
  38. echo $small;
  39.  
  40. function some_function($string){
  41. $string = substr($string,0,100);
  42. $string = substr($string,0,strrpos($string," "));
  43. return $string;
  44. }
  45. ?>
  46.  
  47. list($short) = explode("n",wordwrap($string,100));
  48.  
  49. function text_cut($text, $length = 200, $dots = true) {
  50. $text = trim(preg_replace('#[snrt]{2,}#', ' ', $text));
  51. $text_temp = $text;
  52. while (substr($text, $length, 1) != " ") { $length++; if ($length > strlen($text)) { break; } }
  53. $text = substr($text, 0, $length);
  54. return $text . ( ( $dots == true && $text != '' && strlen($text_temp) > $length ) ? '...' : '');
  55. }
  56.  
  57. /**
  58. * get_words_until() Returns a string of delimited text parts up to a certain length
  59. * If the "words" are too long to limit, it just slices em up to the limit with an ellipsis "..."
  60. *
  61. * @param $paragraph - The text you want to Parse
  62. * @param $limit - The maximum character length, e.g. 160 chars for SMS
  63. * @param string $delimiter - Use ' ' for words and '. ' for sentences (abbreviation bug) :)
  64. * @param null $ellipsis - Use '...' or ' (more)' - Still respects character limit
  65. *
  66. * @return string
  67. */
  68. function get_words_until($paragraph, $limit, $delimiter = ' ', $ellipsis = null)
  69. {
  70. $parts = explode($delimiter, $paragraph);
  71.  
  72. $preview = "";
  73.  
  74. if ($ellipsis) {
  75. $limit = $limit - strlen($ellipsis);
  76. }
  77.  
  78. foreach ($parts as $part) {
  79. $to_add = $part . $delimiter;
  80. if (strlen($preview . trim($to_add)) <= $limit) { // Can the part fit?
  81. $preview .= $to_add;
  82. continue;
  83. }
  84. if (!strlen($preview)) { // Is preview blank?
  85. $preview = substr($part, 0, $limit - 3) . '...'; // Forced ellipsis
  86. break;
  87. }
  88. }
  89.  
  90. return trim($preview) . $ellipsis;
  91. }
  92.  
  93. $big = "This is a sentence that has more than 100 characters in it, and I want to return a string of only full words that is no more than 100 characters!"
  94.  
  95. $small = get_words_until($big, 100);
  96.  
  97. php > $ali = "ali veli krbin yz doksan esikesiksld sjkas laksjald lksjd asldkjadlkajsdlakjlksjdlkaj aslkdj alkdjs akdljsalkdj ";
  98. php > list($short) = explode("n",wordwrap($ali ,50));
  99. php > var_dump($short);
  100. string(42) "ali veli krbin yz doksan esikesiksld sjkas"
  101. php > $ali ='';
  102. php > list($short) = explode("n",wordwrap($ali ,50));
  103. php > var_dump($short);
  104. string(0) ""
  105.  
  106. //trim message to 100 characters, regardless of where it cuts off
  107. $msgTrimmed = mb_substr($var,0,100);
  108.  
  109. //find the index of the last space in the trimmed message
  110. $lastSpace = strrpos($msgTrimmed, ' ', 0);
  111.  
  112. //now trim the message at the last space so we don't cut it off in the middle of a word
  113. echo mb_substr($msgTrimmed,0,$lastSpace)
  114.  
  115. function str_limit($str, $len = 100, $end = '...')
  116. {
  117. if(strlen($str) < $len)
  118. {
  119. return $str;
  120. }
  121.  
  122. $str = preg_replace("/s+/", ' ', str_replace(array("rn", "r", "n"), ' ', $str));
  123.  
  124. if(strlen($str) <= $len)
  125. {
  126. return $str;
  127. }
  128.  
  129. $out = '';
  130. foreach(explode(' ', trim($str)) as $val)
  131. {
  132. $out .= $val . ' ';
  133.  
  134. if(strlen($out) >= $len)
  135. {
  136. $out = trim($out);
  137. return (strlen($out) == strlen($str)) ? $out : $out . $end;
  138. }
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement