0

I have this code:

  $feature_title = trim($array_item[0]);
                                $feature_value ="";
                                if(isset($array_item[1]))
                                {
                                    $feature_value = trim($array_item[1]);
                                }

                                if($feature_title!="" && $feature_value!="" )
                                {
                                    $t= $this->addProductFeature($product->id,$default_language_id, $feature_title , $feature_value);
                                }

Problem is that sometimes the $feature_value is too long. I would like to to limit the output to 200 chars and thought that "trim" would be enough but it is not.

Any ideas ?

user3292026
  • 80
  • 1
  • 10
  • 1
    Possible duplicate of https://stackoverflow.com/questions/3019285/limit-string-length – Nima Sep 25 '17 at 19:42
  • try using the substr(); function. – S.Gartmeier Sep 25 '17 at 19:43
  • 1
    Possible duplicate of [Limit String Length](https://stackoverflow.com/questions/3019285/limit-string-length) – trincot Sep 25 '17 at 20:00
  • Depending on how long the text is, it might look better to use CSS `text-overflow: ellipsis;`. Depending on the characters in the string, different strings with the same number of characters will occupy different amounts of space. – Don't Panic Sep 25 '17 at 20:15
  • I came up with this code but it does not seem to work 'if(strlen($feature_value) > 255){ $feature_value = substr($feature_value, 250); }' – user3292026 Sep 26 '17 at 05:06
  • `trim` does not limit the amount of content, it just remove whitespaces before and after a string. – Herry Potei Sep 26 '17 at 05:12
  • @MammaMia that is why I put it under '$feature_value ="";' But I guess I am doing something wrong – user3292026 Sep 26 '17 at 06:44

1 Answers1

0

I finally changed

 $t= $this->addProductFeature($product->id,$default_language_id, $feature_title , $feature_value);

to

$t= $this->addProductFeature($product->id,$default_language_id, $feature_title , substr($feature_value,0,255));

Hope I didn't brake anything. Seems to work though Thanks everyone

user3292026
  • 80
  • 1
  • 10