{"id":4705,"date":"2013-04-10T15:21:46","date_gmt":"2013-04-10T15:21:46","guid":{"rendered":"https:\/\/www.techrounder.com\/blog\/php-function-to-limit-content-with-complete-word\/"},"modified":"2026-03-07T21:25:16","modified_gmt":"2026-03-07T15:55:16","slug":"php-function-to-limit-content-with-complete-word","status":"publish","type":"post","link":"https:\/\/www.techrounder.com\/blog\/php-function-to-limit-content-with-complete-word\/","title":{"rendered":"PHP Function to limit content with complete word"},"content":{"rendered":"<p>The following function <a href=\"https:\/\/www.techrounder.com\/blog\/apps\/how-betterhelp-mobile-app-can-be-used-for-online-counseling\">can be used<\/a> to limit the content with a complete word rather than broken words.<\/p>\n<pre lang=\"php\">function casper_content_limiter( $text, $length = 64, $tail=\"\" ) {\r\n\t$text =\ttrim($text);\r\n\t$txtl =\tstrlen($text);\r\n\tif( $txtl &gt; $length ) {\r\n\t\tfor( $i = 1; $text[$length-$i] != \" \"; $i++ ) {\r\n\t\t\tif( $i == $length ) {\r\n\t\t\t\treturn substr( $text, 0, $length ) . $tail;\r\n\t\t\t}\r\n\t\t}\r\n\t\t$text =\tsubstr( $text, 0, $length-$i+1 ) . $tail;\r\n\t}\r\n\treturn $text;\r\n}<\/pre>\n<p>In wordPress paste the above function in the function.php file in the theme folder. After that call the function from the template file or any other file where you want to limit the content by <\/p>\n<pre lang=\"php\">$limited_content = casper_content_limiter( $content, 80, '...' );<\/pre>\n<p>Now <strong>$limited_content<\/strong> contains limited content.<br \/>\nIn the function, first parameter, &#8216;$content&#8217; is the actual content which wants to be limited.<br \/>\nSecond parameter is the limit count. ie characters to be limited.<br \/>\nThird parameter is the string\/character to be appended at the end of the limited text. here it is &#8216;&#8230;&#8217;.<\/p>\n","protected":false},"excerpt":{"rendered":"The following function can be used to limit the content with a complete word rather than broken words.&hellip;","protected":false},"author":2,"featured_media":1482,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"csco_display_header_overlay":false,"csco_singular_sidebar":"","csco_page_header_type":"","csco_page_load_nextpost":"","csco_post_video_location":[],"csco_post_video_location_hash":"","csco_post_video_url":"","csco_post_video_bg_start_time":0,"csco_post_video_bg_end_time":0,"csco_post_video_bg_volume":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-4705","post","type-post","status-publish","format-standard","has-post-thumbnail","category-webtips","cs-entry","cs-video-wrap"],"_links":{"self":[{"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/posts\/4705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/comments?post=4705"}],"version-history":[{"count":1,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/posts\/4705\/revisions"}],"predecessor-version":[{"id":9039,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/posts\/4705\/revisions\/9039"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/media\/1482"}],"wp:attachment":[{"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/media?parent=4705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/categories?post=4705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/tags?post=4705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}