[ Solved -8 Answers] JAVASCRIPT – Get first n characters of a string
Table Of Content
How can we get the first n characters of a string in PHP? What’s the fastest way to trim a string to a specific number of characters, and append ‘…’ if needed?
//The simple version for 10 Characters from the beginning of the string.
[pastacode lang=”javascript” manual=”%24string%20%3D%20substr(%24string%2C0%2C10).’…%E2%80%98%0A” message=”javascript code” highlight=”” provider=”manual”/]Update:
- Based on suggestion for checking length (and also ensuring similar lengths on trimmed and untrimmed strings):
- So you will get a string of max 13 characters; either 13 (or less) normal characters or 10 characters followed by ‘…’
Update 2:
Or as function:
[pastacode lang=”javascript” manual=”function%20truncate(%24string%2C%20%24length%2C%20%24dots%20%3D%20%22…%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” message=”javascript code” highlight=”” provider=”manual”/] [ad type=”banner”]- We prefer this function which prevents breaking the string in the middle of a word using the wordwrap function:
- This has been built into PHP since version 4.0.6.
from the docs: http://www.php.net/manual/en/function.mb-strimwidth.php
[pastacode lang=”javascript” manual=”echo%20mb_strimwidth(%22Hello%20World%22%2C%200%2C%2010%2C%20%22…%22)%3B%0A%0A%2F%2F%20outputs%20Hello%20W…%0A” message=”javascript code” highlight=”” provider=”manual”/] [ad type=”banner”]- The Multibyte extension can come in handy, if you need control over the string charset.
- Sometimes, you need to limit the string to the last complete word ie: you don’t want the last word to be broken instead you stop with the second last word.
- eg: we need to limit “This is my String” to 6 chars but instead of ‘This i…” we want it to be ‘This…” ie we will skip that broken letters in the last word.
- If you want to cut being careful to don’t split words you can do the following
- if $str is shorter than $n_chars returns it untouched.
- If $str is equal to $n_chars returns it as is as well.
- 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.
NOTE: be aware that this method will remove all tags in case of HTML.
- substr() would be best, you’ll also want to check the length of the string first.
- wordwrap won’t trim the string down, just split it up…
- Here we use function:
- you just call the function with string and limit.
- eg: str_short(“hahahahahah”,5);
- it will cut of your string and add “…” at the end.
Sample code:
[pastacode lang=”javascript” manual=”function%20cutAfter(%24string%2C%20%24len%20%3D%2030%2C%20%24append%20%3D%20’…’)%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” message=”javascript code” highlight=”” provider=”manual”/]

