Created
December 12, 2012 05:23
-
-
Save bordoni/4265065 to your computer and use it in GitHub Desktop.
A WordPress group of functions for Cropping strings based on some settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Count the words in a string, remove all the html before counting | |
| * | |
| * @since 0.1a | |
| * @access public | |
| * | |
| * @param string $text (required) Any kind of string works, even with html | |
| * Default: 'null'. | |
| * @param string $type (required) The kind of return that you want, could be just an int or an array | |
| * Possible values: '0','1'. | |
| * Default: '0'. | |
| * | |
| * @return '$text' int/array | |
| */ | |
| function wordcount($text=null,$type=0) { | |
| if(!isset($text)) | |
| return null; | |
| $text=explode(' ',preg_replace('/\s+/', ' ',wp_filter_nohtml_kses($text))); | |
| if($type=0) | |
| return sizeof($text); | |
| elseif($type=1) | |
| return $text; | |
| } | |
| /** | |
| * Count the characters in a string, remove all the html before counting | |
| * | |
| * @since 0.1a | |
| * @access public | |
| * | |
| * @param string $text (required) Any kind of string works, even with html | |
| * Default: 'null'. | |
| * | |
| * @return int | |
| */ | |
| function charcount($text=null) { | |
| if(!isset($text)) | |
| return null; | |
| return strlen(preg_replace('/\s+/', ' ',wp_filter_nohtml_kses($text))); | |
| } | |
| /** | |
| * Limit a string by words using the function 'wordcount' to count the number of words and pair with the '$limit' | |
| * | |
| * @since 0.1a | |
| * @access public | |
| * | |
| * @param string $text (required) The string to limit | |
| * Default: 'null'. | |
| * @param int $limit (required) The limit of words | |
| * Default: 'null'. | |
| * | |
| * @return '$text' string | |
| */ | |
| function wordlimit($text=false,$limit=false) { | |
| if( !$text ) | |
| return false; | |
| if( !is_numeric($limit) ) | |
| return $text; | |
| $limit = absint($limit); | |
| $text_a = wordcount($text,1); | |
| $c = sizeof($text_a); | |
| if ( $c>$limit ) { | |
| for ( $i = $limit; $i <= $c; $i++) unset($text_a[$i]); | |
| $text = implode(' ',$text_a); | |
| } | |
| return $text; | |
| } | |
| /** | |
| * Limit a string by characters using the function 'charcount' to count the chars and pair with the '$limit' | |
| * | |
| * @since 0.1a | |
| * @access public | |
| * | |
| * @param string $text (required) The string to limit | |
| * Default: 'null'. | |
| * @param int $limit (required) The limit of words | |
| * Default: 'null'. | |
| * | |
| * @return '$text' string | |
| */ | |
| function charlimit($text=false,$limit=false) { | |
| if(!$text) | |
| return false; | |
| if( !is_numeric($limit) ) | |
| return $text; | |
| $limit = absint($limit); | |
| $c = charcount($text); | |
| if ($c>$limit) | |
| $text = mb_substr($text,0,$limit); | |
| return $text; | |
| } | |
| /** | |
| * Function to limit(words or characters) a text | |
| * | |
| * @since 0.1 | |
| * @access public | |
| * | |
| * @param string/array $args The args to make the function works | |
| * - 'limit' (required) false/int Using false to just append the 'before' and 'after' html or an int to limit the size of the text | |
| * Possible values: 'false', ints. | |
| * Default: 'null'. | |
| * - 'before' (optional) string An text/html to append before the excerpt | |
| * Default: ''. | |
| * - 'after' (optional) string An text/html to append after the excerpt | |
| * Default: ''. | |
| * - 'echo' (required) bool Echos the text or just return the value | |
| * Default: 'true'. | |
| * - 'echo' (required) bool The text to be limited | |
| * Default: ''. | |
| * - 'type' (required) string Limit the excerpt size by word or char | |
| * Possible values: 'word', 'char'. | |
| * Default: 'word'. | |
| * | |
| * @return 'text' string Might echo or return based on the $args | |
| */ | |
| function cropstring($args) { | |
| $defaults = array ( | |
| 'limit' => null, | |
| 'before' => '', | |
| 'after' => '', | |
| 'content' => '', | |
| 'echo' => true, | |
| 'by' => 'word' | |
| ); | |
| $args=wp_parse_args($args, $defaults); | |
| extract($args, EXTR_SKIP); | |
| if(!isset($limit)) | |
| if($echo==true) echo $content; else return $content; | |
| if(!is_numeric($limit)) | |
| if($echo==true) echo $content; else return $content; | |
| if(!isset($by)) | |
| $by='word'; | |
| if ($by=='char') | |
| $count=charcount($content); | |
| else | |
| $count=count(wordcount($content)); | |
| if ($count>$limit){ | |
| if ($by=='char') | |
| $cropped=charlimit($content, $limit); | |
| else | |
| $cropped=wordlimit($content, $limit); | |
| } | |
| if(isset($cropped)) | |
| $content = $before.$cropped.$after; | |
| if($echo==true) echo $content; else return $content; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment