Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $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!"
- $small = some_function($big);
- $small = "This is a sentence that has more than 100 characters in it, and I want to return a string of only"
- $pos=strpos($content, ' ', 200);
- substr($content,0,$pos );
- //truncate a string only at a whitespace (by nogdog)
- function truncate($text, $length) {
- $length = abs((int)$length);
- if(strlen($text) > $length) {
- $text = preg_replace("/^(.{1,$length})(s.*|$)/s", '\1...', $text);
- }
- return($text);
- }
- function limitString($string, $limit = 100) {
- // Return early if the string is already shorter than the limit
- if(strlen($string) < $limit) {return $string;}
- $regex = "/(.{1,$limit})b/";
- preg_match($regex, $string, $matches);
- return $matches[1];
- }
- function truncate($str, $len) {
- $tail = max(0, $len-10);
- $trunk = substr($str, 0, $tail);
- $trunk .= strrev(preg_replace('~^..+?[s,:]b|^...~', '...', strrev(substr($str, $tail, $len-$tail))));
- return $trunk;
- }
- <?PHP
- $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!";
- $small = some_function($big);
- echo $small;
- function some_function($string){
- $string = substr($string,0,100);
- $string = substr($string,0,strrpos($string," "));
- return $string;
- }
- ?>
- list($short) = explode("n",wordwrap($string,100));
- function text_cut($text, $length = 200, $dots = true) {
- $text = trim(preg_replace('#[snrt]{2,}#', ' ', $text));
- $text_temp = $text;
- while (substr($text, $length, 1) != " ") { $length++; if ($length > strlen($text)) { break; } }
- $text = substr($text, 0, $length);
- return $text . ( ( $dots == true && $text != '' && strlen($text_temp) > $length ) ? '...' : '');
- }
- /**
- * get_words_until() Returns a string of delimited text parts up to a certain length
- * If the "words" are too long to limit, it just slices em up to the limit with an ellipsis "..."
- *
- * @param $paragraph - The text you want to Parse
- * @param $limit - The maximum character length, e.g. 160 chars for SMS
- * @param string $delimiter - Use ' ' for words and '. ' for sentences (abbreviation bug) :)
- * @param null $ellipsis - Use '...' or ' (more)' - Still respects character limit
- *
- * @return string
- */
- function get_words_until($paragraph, $limit, $delimiter = ' ', $ellipsis = null)
- {
- $parts = explode($delimiter, $paragraph);
- $preview = "";
- if ($ellipsis) {
- $limit = $limit - strlen($ellipsis);
- }
- foreach ($parts as $part) {
- $to_add = $part . $delimiter;
- if (strlen($preview . trim($to_add)) <= $limit) { // Can the part fit?
- $preview .= $to_add;
- continue;
- }
- if (!strlen($preview)) { // Is preview blank?
- $preview = substr($part, 0, $limit - 3) . '...'; // Forced ellipsis
- break;
- }
- }
- return trim($preview) . $ellipsis;
- }
- $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!"
- $small = get_words_until($big, 100);
- php > $ali = "ali veli krbin yz doksan esikesiksld sjkas laksjald lksjd asldkjadlkajsdlakjlksjdlkaj aslkdj alkdjs akdljsalkdj ";
- php > list($short) = explode("n",wordwrap($ali ,50));
- php > var_dump($short);
- string(42) "ali veli krbin yz doksan esikesiksld sjkas"
- php > $ali ='';
- php > list($short) = explode("n",wordwrap($ali ,50));
- php > var_dump($short);
- string(0) ""
- //trim message to 100 characters, regardless of where it cuts off
- $msgTrimmed = mb_substr($var,0,100);
- //find the index of the last space in the trimmed message
- $lastSpace = strrpos($msgTrimmed, ' ', 0);
- //now trim the message at the last space so we don't cut it off in the middle of a word
- echo mb_substr($msgTrimmed,0,$lastSpace)
- function str_limit($str, $len = 100, $end = '...')
- {
- if(strlen($str) < $len)
- {
- return $str;
- }
- $str = preg_replace("/s+/", ' ', str_replace(array("rn", "r", "n"), ' ', $str));
- if(strlen($str) <= $len)
- {
- return $str;
- }
- $out = '';
- foreach(explode(' ', trim($str)) as $val)
- {
- $out .= $val . ' ';
- if(strlen($out) >= $len)
- {
- $out = trim($out);
- return (strlen($out) == strlen($str)) ? $out : $out . $end;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement