-2

I am working on this piece of code that will take my string and make it 200 characters long and then after that remove the last white space so my string will look like this 'This is my' instead of 'This is my stri'

This is what I got thus far:

$description = substr($value['description'], 0, 200);

and now I want it to remove the last white space in the string. After googleing it, all I found was the php trim function, but nothing about removing the last one, just all..

any tips?

3 Answers3

0
 function mycoolstring($longstring)
      {
          $stringarray=array();
          if(strlen($longstring)>=200){
          $stringarray=explode(" ",substr($longstring,0,200));
          }
          else{
          $stringarray=explode(" ",$longstring);
          }
          $output=stringarray[0];
          for($i=1;$i<count($stringarray)-1;$i++;
             {
                $output.=" " . $stringarray[$i];
             }
          return $output;
      }
  echo mycoolstring($value['discription']);

This may be a weired logic but it should work. Anyone see errors please kindly tell me or edit it. :)

Poomrokc The 3years
  • 1,099
  • 2
  • 10
  • 23
0

If you want it to make it 200 characters long but if you don't want last word to be only partial it should be something like that:

<?php
   mb_internal_encoding('UTF-8');

   $value['description'] = 'avbd wreiuui ewrwrewre';

   $desiredLength = 8; 

   if (mb_strlen($value['description']) <=  $desiredLength) {
     $description = $value['description'];           
   }
   else {       
     $description = mb_substr($value['description'], 0, mb_strpos($value['description'],' ',$desiredLength-1));
   }


   echo $description."<br />";
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
-1
function firstXChars($string, $chars = 100)  { 
    preg_match('/^.{0,' . $chars. '}(?:.*?)\b/iu', $string, $matches); 
    return $matches[0]; 
} 


echo firstXChars($value['description'], 200);

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Kinda curious about the downvotes. This splits the string at the end of the word with no whitespace at the end. Is this just downvoting because people don't like the question? Or don't like a high rep user answering it? – John Conde May 08 '14 at 14:57
  • 1
    (Disclaimer: I didn't vote on this answer.) But I think this is definitely better: `if (strlen($string) > 200) { echo substr($string, 0, strrpos(substr($string, 0, 200), ' ')); }`. – Amal Murali May 08 '14 at 15:14
  • 1
    FYI, code-only answers are often automatically flagged as low-quality. That might be the reason for the downvotes as well. Adding a couple of sentences explaining your answer would solve both problems. – sgress454 May 08 '14 at 15:27
  • 1
    @AD7six: I knew there's a duplicate somewhere, so I didn't answer. Found it now. Kind of surprised to see the [exact same solution](http://stackoverflow.com/a/4665347/) on another thread. Voting to close as a dupe now :-) – Amal Murali May 08 '14 at 15:32