{"id":3362,"date":"2017-04-01T16:33:10","date_gmt":"2017-04-01T11:03:10","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=3362"},"modified":"2017-04-01T16:33:10","modified_gmt":"2017-04-01T11:03:10","slug":"get-first-n-characters-string","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/get-first-n-characters-string\/","title":{"rendered":"[ Solved -8 Answers] JAVASCRIPT &#8211; Get first n characters of a string"},"content":{"rendered":"<p><label class=\"label label-warning\">PROBLEM:<\/label><\/p>\n<p>How can we get the first n characters of a string in PHP? What\u2019s the fastest way to trim a string to a specific number of characters, and append \u2018\u2026\u2019 if needed?<\/p>\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<p>\/\/The simple version for 10 Characters from the beginning of the string.<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%24string%20%3D%20substr(%24string%2C0%2C10).\u2019\u2026%E2%80%98%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"update\"><span style=\"color: #ff6600;\"><b>Update:<\/b><\/span><\/h4>\n<ul>\n<li>Based on suggestion for checking length (and also ensuring similar lengths on trimmed and untrimmed strings):<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%24string%20%3D%20(strlen(%24string)%20%3E%2013)%20%3F%20substr(%24string%2C0%2C10).\u2019\u2026\u2019%20%3A%20%24string%3B%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>So you will get a string of max 13 characters; either 13 (or less) normal characters or 10 characters followed by \u2018\u2026\u2019<\/li>\n<\/ul>\n<h4 id=\"update-2\"><span style=\"color: #993300;\"><b>Update 2:<\/b><\/span><\/h4>\n<p>Or as function:<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dfunction%20truncate(%24string%2C%20%24length%2C%20%24dots%20%3D%20%22\u2026%22)%20%0A%7B%0A%20%20%20%20return%20(strlen(%24string)%20%3E%20%24length)%20%3F%20substr(%24string%2C%200%2C%20%24length%20-%20strlen(%24dots))%20.%20%24dots%20%3A%20%24string%3B%0A%7D%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<ul>\n<li>We prefer this function which prevents breaking the string in the middle of a word using the wordwrap function:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dfunction%20truncate(%24string%2C%24length%3D100%2C%24append%3D%22%26hellip%3B%22)%20%0A%7B%0A%20%20%24string%20%3D%20trim(%24string)%3B%0A%0A%20%20if(strlen(%24string)%20%3E%20%24length)%20%0A%7B%0A%20%20%20%20%24string%20%3D%20wordwrap(%24string%2C%20%24length)%3B%0A%20%20%20%20%24string%20%3D%20explode(%22%5Cn%22%2C%20%24string%2C%202)%3B%0A%20%20%20%20%24string%20%3D%20%24string%5B0%5D%20.%20%24append%3B%0A%20%20%7D%0A%0A%20%20return%20%24string%3B%0A%7D%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<ul>\n<li>This has been built into PHP since version 4.0.6.<\/li>\n<\/ul>\n<p>from the docs: http:\/\/www.php.net\/manual\/en\/function.mb-strimwidth.php<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201decho%20mb_strimwidth(%22Hello%20World%22%2C%200%2C%2010%2C%20%22\u2026%22)%3B%0A%0A%2F%2F%20outputs%20Hello%20W\u2026%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<ul>\n<li>The Multibyte extension can come in handy, if you need control over the string charset.<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%24charset%20%3D%20\u2019UTF-8\u2019%3B%0A%24length%20%3D%2010%3B%0A%24string%20%3D%20\u2019Hai%20to%20yoo!%20I%20like%20yoo%20soo!\u2019%3B%0Aif(mb_strlen(%24string%2C%20%24charset)%20%3E%20%24length)%20%0A%7B%0A%20%20%24string%20%3D%20mb_substr(%24string%2C%200%2C%20%24length%20-%203%2C%20%24charset)%20.%20\u2019\u2026\u2019%3B%0A%7D%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n<ul>\n<li>Sometimes, you need to limit the string to the last complete word ie: you don\u2019t want the last word to be broken instead you stop with the second last word.<\/li>\n<li>eg: we need to limit \u201cThis is my String\u201d to 6 chars but instead of \u2018This i\u2026\u201d we want it to be \u2018This\u2026\u201d ie we will skip that broken letters in the last word.<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dclass%20Fun%20%0A%7B%0A%0A%20%20%20%20public%20function%20limit_text(%24text%2C%20%24len)%20%0A%7B%0A%20%20%20%20%20%20%20%20if%20(strlen(%24text)%20%3C%20%24len)%20%0A%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%24text%3B%0A%7D%0A%20%20%20%20%20%20%20%20%24text_words%20%3D%20explode(\u2018%20\u2019%2C%20%24text)%3B%0A%20%20%20%20%20%20%20%20%24out%20%3D%20null%3B%0A%20%20foreach%20(%24text_words%20as%20%24word)%20%0A%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20((strlen(%24word)%20%3E%20%24len)%20%26%26%20%24out%20%3D%3D%20null)%0A%20%7B%0A%20%20return%20substr(%24word%2C%200%2C%20%24len)%20.%20%22\u2026%22%3B%0A%20%7D%0A%20%20if%20((strlen(%24out)%20%2B%20strlen(%24word))%20%3E%20%24len)%20%0A%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20%24out%20.%20%22\u2026%22%3B%0A%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%24out.%3D%22%20%22%20.%20%24word%3B%0A%20%7D%0A%20%20%20%20%20%20%20%20return%20%24out%3B%0A%20%20%20%20%7D%0A%0A%7D%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<ul>\n<li>If you want to cut being careful to don\u2019t split words you can do the following<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dfunction%20ellipse(%24str%2C%24n_chars%2C%24crop_str%3D\u2019%20%5B\u2026%5D\u2019)%0A%7B%0A%20%20%20%20%24buff%3Dstrip_tags(%24str)%3B%0A%20%20%20%20if(strlen(%24buff)%20%3E%20%24n_chars)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%24cut_index%3Dstrpos(%24buff%2C\u2019%20\u2019%2C%24n_chars)%3B%0A%20%20%20%20%20%20%20%20%24buff%3Dsubstr(%24buff%2C0%2C(%24cut_index%3D%3D%3Dfalse%3F%20%24n_chars%3A%20%24cut_index%2B1)).%24crop_str%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20%24buff%3B%0A%7D%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<ul>\n<li>if $str is shorter than $n_chars returns it untouched.<\/li>\n<li>If $str is equal to $n_chars returns it as is as well.<\/li>\n<li>if $str is longer than $n_chars then it looks for the next space to cut or (if no more spaces till the end) $str gets cut rudely instead at $n_chars.<\/li>\n<\/ul>\n<p>NOTE: be aware that this method will remove all tags in case of HTML.<\/p>\n<p><label class=\"label label-info\">SOLUTION 6:<\/label><\/p>\n<ul>\n<li>substr() would be best, you\u2019ll also want to check the length of the string first.<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%24str%20%3D%20\u2019someLongString\u2019%3B%0A%24max%20%3D%207%3B%0A%0Aif(strlen(%24str)%20%3E%20%24max)%20%0A%7B%0A%20%20%20%24str%20%3D%20substr(%24str%2C%200%2C%20%24max)%20.%20\u2019\u2026\u2019%3B%0A%7D%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>wordwrap won\u2019t trim the string down, just split it up\u2026<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 7:<\/label><\/p>\n<ul>\n<li>Here we use function:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%20function%20str_short(%24string%2C%24limit)%0A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24len%3Dstrlen(%24string)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if(%24len%3E%24limit)%0A%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%24to_sub%3D%24len-%24limit%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%24crop_temp%3Dsubstr(%24string%2C0%2C-%24to_sub)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20return%20%24crop_len%3D%24crop_temp.%22\u2026%22%3B%0A%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20%24string%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<ul>\n<li>\u00a0you just call the function with string and limit.<\/li>\n<li>eg: str_short(\u201chahahahahah\u201d,5);<\/li>\n<li>it will cut of your string and add \u201c\u2026\u201d at the end.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 8:<\/label><\/p>\n<h4 id=\"sample-code\"><span style=\"color: #ff6600;\"><b>Sample code:<\/b><\/span><\/h4>\n[pastacode lang=\u201djavascript\u201d manual=\u201dfunction%20cutAfter(%24string%2C%20%24len%20%3D%2030%2C%20%24append%20%3D%20\u2019\u2026\u2019)%20%0A%7B%0A%20%20%20%20%20%20%20%20return%20(strlen(%24string)%20%3E%20%24len)%20%3F%20%0A%20%20%20%20%20%20%20%20%20%20substr(%24string%2C%200%2C%20%24len%20-%20strlen(%24append))%20.%20%24append%20%3A%20%0A%20%20%20%20%20%20%20%20%20%20%24string%3B%0A%7D%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get first n characters of a string &#8211; The Multibyte extension can come in if you need control over the string charset.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[275],"tags":[6254,2134,4089,4153,1128,3066,6255,6253,6256,4088,4087],"class_list":["post-3362","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-getting-the-first-character-of-a-string-with-str0","tag-how-do-i-check-if-a-string-contains-a-specific-word-in-php","tag-how-do-i-get-a-consistent-byte-representation-of-strings-in-c-without-manually-specifying-an-encoding","tag-how-do-i-make-the-first-letter-of-a-string-uppercase-in-javascript","tag-how-to-check-whether-a-string-contains-a-substring-in-javascript","tag-how-to-convert-a-string-to-an-int-in-java","tag-how-to-get-the-last-char-of-a-string-in-php","tag-is-there-a-way-to-substring-a-string-in-python","tag-php-string-length","tag-split-a-string-in-c","tag-what-is-the-difference-between-string-and-string-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=3362"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3362\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=3362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=3362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=3362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}