Another note, if length is negative and start offsets the same position as length, length (yet again) will have the effect as being set as 0. (Of course, as mentioned in the manual, when length is negative it actually represents the position before it)

I recently ran across a situation where I need to strip a heavily nested html list such that only the top level was preserved. I started with a regular expression solution, but found that I kept matching the wrong closing ul with an outer opening ul.

$max_length=10;//the max number of characters you want to display$too_long_string="BLAH BLAH BLAH BLAH BLAH etc.";//the string you want to shorten (if it's longer than the $limit)$shorter_string=add_3_dots($too_long_string,"...",$max_length);

My problem was that substr_replace() always added $replacement, so i wrote my own function.This function only adds $replacement, if substr() took action.The parameter $length is optional - like substr()'s.Or I was too stupid using $start and $length...

If your string is not long enough to meet what you specify in start and length then the replacement string is added towards the end of the string.

I wanted to replace the end of the string with ... if the string was too long to display (for instance article preview on a website). The problem was that my string was sometimes not that long and it still added the replacement string. So I wrote a function to replace substr_replace in my website:

This can be useful when you need to replace parts of multibyte strings like strings encoded with utf-8. There isn't a multibute variant for substr_replace(), but for php substr() there is mb_substr(). For more information on multibyte strings see http://nl3.php.net/manual/en/ref.mbstring.php

The preemptive test to see if $string is "too long" shouldn't add strlen($replacement) to $max. $max should represent the absolute maximum length of string returned. The size of the $replacement is irrelevant in that determination.

The rest of the function (unchanged below) operates as defined above. Meaning, the size of the $replacement is subtracted from the $max, so that the returned string is exactly the length of $max.

According to my own measurements, the regex in ONLY faster for when strlen($chars) == 1; for longer strings, my routine is faster. What does it do? Let's say you want to remove the period, the comma and the exclamation mark from a string, like so:$result = RemoveChars("Isn't this, like, totally neat..!?", ".?!");The str_pad function creates a string equal in length to the string that contains the character to be removed, but consisting only of the first character of that string:The input is ".,!"The output is "..."The strtr function translates all characters in the string-to-be-processed ("Isn't this...") that also occur in the input (".,!") to the characters in the same position in the output ("..."). In other words:Isn't this, like, totally neat..!?becomesIsn't this. like. totally neat....Finally, the first character from the input (".,!") which happens to be, again, the period, is removed from that string by the str_replace call:Isn't this like totally neat?The function needs to check is $chars has at least one character, or else the str_pad function will fail. If it's empty, then the unprocessed string is returned.

I like the truncate function below...however, I found a few issues. Particularly if you have content that may have any kind of punctuation in it (?, !, ?!?, --, ..., .., ;, etc.)

The older function would end up looking like "blah blah?..." or "blah blah,..." which doesn't look so nice to me...

Here's my fix. It removes all trailing punctuation (that you include in the $punctuation string below) and then adds an ellipse. So even if it has an ellipse with 3 dots, 2 dots, 4 dots, it'll be removed, then re-added.

I also needed a sort of "middle" truncate. The above function truncates around the end, but if you want to truncate around the middle (ie "Hello this is a long string." --> "Hello this ... long string.") you can use this (requires the truncate function):

The comment by geniusdex is a good one. Short, simple functions are the best. But if the string is not longer than the limit set, NOTHING is returned. Here is the function re-done to always return a string:

Just to add to the examples, if replacement is longer than length, only the length number of chars are removed from string and all of replacement is put in its place, and therefor strlen($string) is inreased.

Hey everyone, I was noticing that there are a lot of ways below that people are using to write their own string truncation functions, but it kinda seemed like a lot of them went a bit too far out to make any sense to a n00b. Not that I am one anymore, but I though I'd add a note on this topic myself, in hopes that it might help others understand things a little better.

Here's a concept that some people don't know about, or remember to use often enough; You can actually pull individual characters out of a string by referencing that string as though it were an array. Example: If I have the string $s = 'cat', I can use $s[0] to actually get out only the first character of that string, 'c'. I use that same principle below, but I just use a loop to iterate through a string and add the characters to the output variable one by one until the $lenth param has been reached, or until the end of the string.