Cutting a string in PHP
07:18 11 Apr 2011

I have a string. Sometimes it is longer than 50 characters, sometimes shorter. If it is longer I want to cut it to 50 (or the nearest '.' after 50 characters if possible.

I currently use strlen to check, then copy each character into a new string using the strings array until I reach 50 (in a for loop). This seems like a bad way to do it and slow. I have no way to do the up to '.' part so far...

  • Is their a better way to cut the string?
  • How can one do the '.' part?
php string