You are here:  » Show only the first two words from product name

Support Forum



Show only the first two words from product name

Submitted by chrisst1 on Sat, 2010-11-06 10:50 in

Hi David

Hope you are keeping well, after several years with this script I thought its time to upgrade and improve.

I am trying to show only the first two words from a product name using:



Trouble is it leaves a space before, ideally what I am trying to do I set a fixed amount of characters and only show one, two or tree words up to the character limit but not a partial word which substr($product["name"],0,20) does.

Not sure whether I am getting the above right, could you point me in the right direction please?

Chris

Submitted by support on Sat, 2010-11-06 13:48

Hi Chris,

Here's a substr function that will break on the next space after the specified limit:

  function substr_space($text,$length)
  {
    if (strlen($text) > $length)
    {
      $breakOffset = strpos($text," ",$length);
      if ($breakOffset !== false)
      {
        $text = substr($text,0,$breakOffset);
      }
    }
    return $text;
  }

Add the function to the top of your file, and then use:

  $name = $product["name"];
  $name = substr_space($name,20);

Cheers,
David.
--
PriceTapestry.com

Submitted by chrisst1 on Tue, 2010-11-09 14:22

Thanks David

That worked great as always.

I was having a look at your new version and the logo upload, have you thought of adding a merchant description and link field? Also a couple of years back you did a manage reviews page code for us, maybe good to add in.

Chris