Jump to content

Word Trimming Help


mitzleah

Recommended Posts

$wordCount = 0;
$charCount = 0;
$str = "Hello World";
for ($i=0; $i<1000;$i++) {  // no sentence with 50 words is going to be longer than 1000 characters
if ( $str[$i] == " "){  // if the character is a space i.e end of word
  $wordCount ++;
  $charCount ++;
  if($wordCount == 50) {
  break;}
} else {  // if its a letter then it is still in the word
  $charCount ++;
  }
}

$newstr = substr($str, 0, $charCount);

Link to comment
https://forums.phpfreaks.com/topic/21502-word-trimming-help/#findComment-95890
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.