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):
[pastacode lang=”javascript” manual=”%24string%20%3D%20(strlen(%24string)%20%3E%2013)%20%3F%20substr(%24string%2C0%2C10).’…’%20%3A%20%24string%3B%0A” message=”javascript code” highlight=”” provider=”manual”/]
  • 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:
[pastacode lang=”javascript” manual=”function%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” message=”javascript code” highlight=”” provider=”manual”/]

  • 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.
[pastacode lang=”javascript” manual=”%24charset%20%3D%20’UTF-8’%3B%0A%24length%20%3D%2010%3B%0A%24string%20%3D%20’Hai%20to%20yoo!%20I%20like%20yoo%20soo!’%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’…’%3B%0A%7D%0A” message=”javascript code” highlight=”” provider=”manual”/]

  • 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.
[pastacode lang=”javascript” manual=”class%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(‘%20’%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…%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…%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” message=”javascript code” highlight=”” provider=”manual”/]

  • If you want to cut being careful to don’t split words you can do the following
[pastacode lang=”javascript” manual=”function%20ellipse(%24str%2C%24n_chars%2C%24crop_str%3D’%20%5B…%5D’)%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’%20’%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” message=”javascript code” highlight=”” provider=”manual”/] [ad type=”banner”]
  • 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.
[pastacode lang=”javascript” manual=”%24str%20%3D%20’someLongString’%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’…’%3B%0A%7D%0A” message=”javascript code” highlight=”” provider=”manual”/]
  • wordwrap won’t trim the string down, just split it up…

  • Here we use function:
[pastacode lang=”javascript” manual=”%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…%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” message=”javascript code” highlight=”” provider=”manual”/] [ad type=”banner”]
  •  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”/]

 

Categorized in: