{"id":952,"date":"2022-07-03T16:20:04","date_gmt":"2022-07-03T15:20:04","guid":{"rendered":"https:\/\/codeflarelimited.com\/blog\/?p=952"},"modified":"2022-07-03T16:20:07","modified_gmt":"2022-07-03T15:20:07","slug":"php-shorten-string-with-three-dots","status":"publish","type":"post","link":"https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/","title":{"rendered":"PHP Shorten String With Three Dots"},"content":{"rendered":"\n<p>Using PHP, we can shorten the length of our string and just replace the rest with three dots. <\/p>\n\n\n\n<p>This is usually needed in a software application, especially when the string length is too long to show to the user.<\/p>\n\n\n\n<p>So we will write a function that is re-usable and which we can always call or use in any other programming.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\"> function trimString($string, $repl, $limit) \n{\n  if(strlen($string) > $limit) \n  {\n    return substr($string, 12, $limit) . $repl; \n  }\n  else \n  {\n    return $string;\n  }\n}\n?>\n\n echo trimString(\"Hello Codeflare\", \"...\", 3);<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-explanation\">Explanation<\/h2>\n\n\n\n<p>The <em><strong>trimString<\/strong><\/em> function we created takes three (3) parameters: <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The string which we want to shorten,<\/li><li>What we want to replace the cut out section with, which in this case is three (3) dots<\/li><li>And the limit or length of string.<\/li><\/ol>\n\n\n\n<p>Next, we check if the length of the given string is greater than the limit which we have set. If it is, we truncate it and append our three dots. If it&#8217;s not, we just return the given string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2>\n\n\n\n<p>This is how we shorten the length of a string and replace it with three (3) dots in PHP. <\/p>\n\n\n\n<p>Let me know what you think in the comments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-see-also\">See Also:<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-image-upload-with-preview-using-php-s-pdo-and-jquery\"><a href=\"https:\/\/codeflarelimited.com\/blog\/image-upload-with-file-preview-using-phps-pdo-and-jquery\/\" target=\"_blank\" rel=\"noreferrer noopener\">Image Upload With Preview Using PHP\u2019s PDO And jQuery<\/a><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-enrol-for-software-development-training\"><a href=\"https:\/\/codeflarelimited.com\" target=\"_blank\" rel=\"noreferrer noopener\">Enrol for Software Development Training<\/a><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>Using PHP, we can shorten the length of our string and just replace the rest with three dots.<\/p>\n","protected":false},"author":1,"featured_media":953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[87,95],"tags":[],"class_list":["post-952","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-software-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP Shorten String With Three Dots<\/title>\n<meta name=\"description\" content=\"Using PHP, we can shorten a string length and just replace the rest with three dots. This is usually needed in a software application\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP Shorten String With Three Dots\" \/>\n<meta property=\"og:description\" content=\"Using PHP, we can shorten a string length and just replace the rest with three dots. This is usually needed in a software application\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/facebook.com\/codeflretech\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-03T15:20:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-03T15:20:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2022\/07\/acab484c8b01db6a422928057ac799ba.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"codeflare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codeflaretech\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/php-shorten-string-with-three-dots\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/php-shorten-string-with-three-dots\\\/\"},\"author\":{\"name\":\"codeflare\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/person\\\/7e65653d49add95629f8c1053c5cd76a\"},\"headline\":\"PHP Shorten String With Three Dots\",\"datePublished\":\"2022-07-03T15:20:04+00:00\",\"dateModified\":\"2022-07-03T15:20:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/php-shorten-string-with-three-dots\\\/\"},\"wordCount\":190,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/php-shorten-string-with-three-dots\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/acab484c8b01db6a422928057ac799ba.jpg\",\"articleSection\":[\"php\",\"software development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/php-shorten-string-with-three-dots\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/php-shorten-string-with-three-dots\\\/\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/php-shorten-string-with-three-dots\\\/\",\"name\":\"PHP Shorten String With Three Dots\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/php-shorten-string-with-three-dots\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/php-shorten-string-with-three-dots\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/acab484c8b01db6a422928057ac799ba.jpg\",\"datePublished\":\"2022-07-03T15:20:04+00:00\",\"dateModified\":\"2022-07-03T15:20:07+00:00\",\"description\":\"Using PHP, we can shorten a string length and just replace the rest with three dots. This is usually needed in a software application\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/php-shorten-string-with-three-dots\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/php-shorten-string-with-three-dots\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/php-shorten-string-with-three-dots\\\/#primaryimage\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/acab484c8b01db6a422928057ac799ba.jpg\",\"contentUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/acab484c8b01db6a422928057ac799ba.jpg\",\"width\":1280,\"height\":720,\"caption\":\"php shorten string with three dots\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/php-shorten-string-with-three-dots\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"php\",\"item\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/php\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PHP Shorten String With Three Dots\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/\",\"name\":\"\",\"description\":\"Sustainable solutions\",\"publisher\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#organization\",\"name\":\"Codeflare Limited\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/codeflare.png\",\"contentUrl\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/codeflare.png\",\"width\":1040,\"height\":263,\"caption\":\"Codeflare Limited\"},\"image\":{\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/#\\\/schema\\\/person\\\/7e65653d49add95629f8c1053c5cd76a\",\"name\":\"codeflare\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g\",\"caption\":\"codeflare\"},\"description\":\"Latest tech news and coding tips.\",\"sameAs\":[\"https:\\\/\\\/codeflarelimited.com\\\/blog\",\"https:\\\/\\\/facebook.com\\\/codeflretech\",\"https:\\\/\\\/instagram.com\\\/codeflaretech\",\"https:\\\/\\\/x.com\\\/codeflaretech\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCuBLtiYqsajHdqw0uyt7Ofw?sub_confirmation=1\"],\"url\":\"https:\\\/\\\/codeflarelimited.com\\\/blog\\\/author\\\/watcher\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP Shorten String With Three Dots","description":"Using PHP, we can shorten a string length and just replace the rest with three dots. This is usually needed in a software application","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/","og_locale":"en_US","og_type":"article","og_title":"PHP Shorten String With Three Dots","og_description":"Using PHP, we can shorten a string length and just replace the rest with three dots. This is usually needed in a software application","og_url":"https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/","article_author":"https:\/\/facebook.com\/codeflretech","article_published_time":"2022-07-03T15:20:04+00:00","article_modified_time":"2022-07-03T15:20:07+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2022\/07\/acab484c8b01db6a422928057ac799ba.jpg","type":"image\/jpeg"}],"author":"codeflare","twitter_card":"summary_large_image","twitter_creator":"@codeflaretech","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/#article","isPartOf":{"@id":"https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/"},"author":{"name":"codeflare","@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/person\/7e65653d49add95629f8c1053c5cd76a"},"headline":"PHP Shorten String With Three Dots","datePublished":"2022-07-03T15:20:04+00:00","dateModified":"2022-07-03T15:20:07+00:00","mainEntityOfPage":{"@id":"https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/"},"wordCount":190,"commentCount":0,"publisher":{"@id":"https:\/\/codeflarelimited.com\/blog\/#organization"},"image":{"@id":"https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/#primaryimage"},"thumbnailUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2022\/07\/acab484c8b01db6a422928057ac799ba.jpg","articleSection":["php","software development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/","url":"https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/","name":"PHP Shorten String With Three Dots","isPartOf":{"@id":"https:\/\/codeflarelimited.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/#primaryimage"},"image":{"@id":"https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/#primaryimage"},"thumbnailUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2022\/07\/acab484c8b01db6a422928057ac799ba.jpg","datePublished":"2022-07-03T15:20:04+00:00","dateModified":"2022-07-03T15:20:07+00:00","description":"Using PHP, we can shorten a string length and just replace the rest with three dots. This is usually needed in a software application","breadcrumb":{"@id":"https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/#primaryimage","url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2022\/07\/acab484c8b01db6a422928057ac799ba.jpg","contentUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2022\/07\/acab484c8b01db6a422928057ac799ba.jpg","width":1280,"height":720,"caption":"php shorten string with three dots"},{"@type":"BreadcrumbList","@id":"https:\/\/codeflarelimited.com\/blog\/php-shorten-string-with-three-dots\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codeflarelimited.com\/blog\/"},{"@type":"ListItem","position":2,"name":"php","item":"https:\/\/codeflarelimited.com\/blog\/php\/"},{"@type":"ListItem","position":3,"name":"PHP Shorten String With Three Dots"}]},{"@type":"WebSite","@id":"https:\/\/codeflarelimited.com\/blog\/#website","url":"https:\/\/codeflarelimited.com\/blog\/","name":"","description":"Sustainable solutions","publisher":{"@id":"https:\/\/codeflarelimited.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codeflarelimited.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codeflarelimited.com\/blog\/#organization","name":"Codeflare Limited","url":"https:\/\/codeflarelimited.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/codeflare.png","contentUrl":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2020\/11\/codeflare.png","width":1040,"height":263,"caption":"Codeflare Limited"},"image":{"@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/codeflarelimited.com\/blog\/#\/schema\/person\/7e65653d49add95629f8c1053c5cd76a","name":"codeflare","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/59cef917c86d965eea581d2747f51bd6382003a68bfce7c8a4dfec98b4cd838d?s=96&d=mm&r=g","caption":"codeflare"},"description":"Latest tech news and coding tips.","sameAs":["https:\/\/codeflarelimited.com\/blog","https:\/\/facebook.com\/codeflretech","https:\/\/instagram.com\/codeflaretech","https:\/\/x.com\/codeflaretech","https:\/\/www.youtube.com\/channel\/UCuBLtiYqsajHdqw0uyt7Ofw?sub_confirmation=1"],"url":"https:\/\/codeflarelimited.com\/blog\/author\/watcher\/"}]}},"jetpack_featured_media_url":"https:\/\/codeflarelimited.com\/blog\/wp-content\/uploads\/2022\/07\/acab484c8b01db6a422928057ac799ba.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts\/952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/comments?post=952"}],"version-history":[{"count":1,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts\/952\/revisions"}],"predecessor-version":[{"id":954,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/posts\/952\/revisions\/954"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/media\/953"}],"wp:attachment":[{"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/media?parent=952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/categories?post=952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeflarelimited.com\/blog\/wp-json\/wp\/v2\/tags?post=952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}