{"id":3892,"date":"2013-06-15T19:05:42","date_gmt":"2013-06-15T19:05:42","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=3892"},"modified":"2013-06-15T19:05:42","modified_gmt":"2013-06-15T19:05:42","slug":"php-return-string-from-string-using-word-limit-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/php-return-string-from-string-using-word-limit-examples\/","title":{"rendered":"PHP Return String From String Using Word Limit Examples"},"content":{"rendered":"<p>Some times, it might be useful to extract string from a given string, based upon the word count. A good example of this is an excerpt of WordPress. As you know, an &#8220;excerpt&#8221; is just a sneak peak of the article. And it is limited by it&#8217;s word count (or any other desired criteria). So how can we achieve the same result in PHP? In this article, I am going to share easy ways to <em><strong>return text from a given text, based upon the word count limit<\/strong><\/em>. So follow along for more info.<\/p>\n<p><!--more--><\/p>\n<h2>How To Limit String Using Word Count<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=3892\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>Let&#8217;s see how to do this with an example.<\/p>\n<h3>Example: Consider the following string on which we apply word limit using PHP<\/h3>\n<pre class=\"lang:xhtml decode:true\">This is a custom string. We are now going to apply PHP functions on this and return only the portion of the string based upon the word count.<\/pre>\n<p>Now let&#8217;s limit the above string to a word count of 10 and then return the result. Use the following code to do so:<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;?php \nfunction word_limiter($string, $limit) \n{ \n\t\/\/break string based upon space\n\t$str_array = explode(\" \", $string); \n\n\t\/\/return the extracted string with our word limit in place\n\treturn implode(\" \", array_splice($str_array, 0, $limit)); \n}\n\n$string = 'This is a custom string. We are now going to apply PHP functions on this and return only the portion of the string based upon the word count.';\n\n$word_limit = 10; \/\/Change this based upon your needs\n\necho word_limiter($string, $word_limit);\n?&gt;<\/pre>\n<h3>\u00a0Output:<\/h3>\n<pre class=\"lang:xhtml decode:true\">This is a custom string. We are now going to<\/pre>\n<p>If you count the number of word in the above Output, you will notice that there are only 10 words. This is exactly what we wanted.<\/p>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=3892\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h5>That&#8217;s it!<\/h5>\n<p>Do you know of any other ways to <em><strong>limit string using word count in PHP<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some times, it might be useful to extract string from a given string, based upon the word count. A good example of this is an excerpt of WordPress. As you know, an &#8220;excerpt&#8221; is just a sneak peak of the article. And it is limited by it&#8217;s word count (or any other desired criteria). So [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":141,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10,13],"tags":[125,33],"class_list":["post-3892","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-tips-and-tricks","category-tips-and-tricks","tag-php-demos-2","tag-php-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-10M","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3892","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=3892"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3892\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=3892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=3892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=3892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}