Announcement

Collapse
No announcement yet.

PHP Ellipsis of descriptions when they contain non alpha characters

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    PHP Ellipsis of descriptions when they contain non alpha characters

    I am using a php ellipsis to shorten product descriptions in the product summary and end with ...

    Setting the encoding of Product Description to strip HTML deals with html code but if the description contains "some quoted text" in quote marks it generates a parser error.

    Can any php wizards help? BTW, I am using php as it is a very javascript heavy site and I am wary of conflicts.

    Example:

    This product description is OK

    !!<Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. <br><b>size: </b>200ml>!!
    !!<<a href="http://www.domainname.co.uk/acatalog/link.html#B"><img src="some-image-120.jpg" width="120" height="60"></a>>!!
    with this elli[psis php code:

    Code:
    <actinic:block php="true" >
    $string = "<actinic:variable encoding="strip" name="ProductDescription" />";
    $length = 190;
    
        if (strlen($string) > $length) {
            //limit hit!
            $string = substr($string,0,($length -3));
                //stop on a word.
                $string = substr($string,0,strrpos($string,' ')).'...';
    
        echo $string;
    }
    	</actinic:block>
    However, putting some quotes round some of the words in the description ("consectetur adipiscing elit, sed do eiusmod tempor incididunt") stops it working producing a parser error:

    !!<Lorem ipsum dolor sit amet, "consectetur adipiscing elit, sed do eiusmod tempor incididunt" ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. <br><b>size: </b>200ml>!!
    !!<<a href="http://www.domainname.co.uk/acatalog/link.html#B"><img src="some-image-120.jpg" width="120" height="60"></a>>!!
    There are thousands of products all with coded content so change is not an option.

    I would be so grateful to be pointed in the right direction.

    Thank you
    Jonathan Chappell
    Website Designer
    SellerDeck Website Designer
    Actinic to SellerDeck upgrades
    Graphicz Limited - www.graphicz.co.uk

    #2
    I think you probably need to escape the control characters. See addslashes

    http://php.net/addslashes
    -----------------------------------------

    First Tackle - Fly Fishing and Game Angling

    -----------------------------------------

    Comment


      #3
      Still struggling. Please can anyone correct my clumsy efforts?
      Code:
      	<actinic:block php="true" >
      	
      $abc = "<actinic:variable encoding="strip" name="ProductDescription" />";	
      $length = 190;
      $string =  addslashes($abc);
          if (strlen($string) > $length) {
              //limit hit!        
              $string = substr($string,0,($length -3));
                  //stop on a word.
                  $string = substr($string,0,strrpos($string,' ')).'...';
      
          echo $string;
      }
      	</actinic:block>
      Jonathan Chappell
      Website Designer
      SellerDeck Website Designer
      Actinic to SellerDeck upgrades
      Graphicz Limited - www.graphicz.co.uk

      Comment


        #4
        Try using heredoc instead of a quoted string:
        Code:
        $abc <<<ENDOFDESCRIPTION
        <actinic:variable encoding="strip" name="ProductDescription" />
        ENDOFDESCRIPTION;
        Best read up on heredoc as there are some gotcha's. http://php.net/manual/en/language.ty...syntax.heredoc
        Norman - www.drillpine.biz
        Edinburgh, U K / Bitez, Turkey

        Comment


          #5
          Thank you Norman - still struggling:

          Is this how you suggest it should be?

          Code:
          <actinic:block php="true" >
          	
          $abc <<<ENDOFDESCRIPTION
          <actinic:variable encoding="strip" name="ProductDescription" />
          ENDOFDESCRIPTION;
          $length = 190;
          $string =  addslashes($abc);
              if (strlen($string) > $length) {
                  //limit hit!        
                  $string = substr($string,0,($length -3));
                      //stop on a word.
                      $string = substr($string,0,strrpos($string,' ')).'...';
          
              echo $string;
          }
          	</actinic:block>
          Jonathan Chappell
          Website Designer
          SellerDeck Website Designer
          Actinic to SellerDeck upgrades
          Graphicz Limited - www.graphicz.co.uk

          Comment


            #6
            My bit looks OK. Why not bung in some diagnostic code?
            Code:
            ENDOFDESCRIPTION;
            echo "ABC" . $abc . "END";
            And look at the generated HTML by Previewing / View Source and search for ABC to see what you have really got.

            UPDATE: I've no Idea why you're using that addslashes line.
            Norman - www.drillpine.biz
            Edinburgh, U K / Bitez, Turkey

            Comment


              #7
              Code:
              <actinic:block php="true" >	
              $abc <<<ENDOFDESCRIPTION
              <actinic:variable encoding="strip" name="ProductDescription" />
              ENDOFDESCRIPTION;
              echo "ABC" . $abc . "END";
              	</actinic:block>
              yields a parse error
              Jonathan Chappell
              Website Designer
              SellerDeck Website Designer
              Actinic to SellerDeck upgrades
              Graphicz Limited - www.graphicz.co.uk

              Comment


                #8
                This:
                Code:
                <actinic:block php="true">
                $str = <<<EOD
                <actinic:variable name="ProductDescription" />
                EOD;
                echo "STR" . $str . "EOD";
                </actinic:block>
                Works for me. However copy / pasting it from here may be intoducing some unwanted characters.

                UPDATE. If you still see errors, try copy / pasting the code from Example #2 on http://php.net/manual/en/language.ty...syntax.heredoc
                Code:
                $str = <<<EOD
                Example of string
                spanning multiple lines
                using heredoc syntax.
                EOD;
                See if it works without error and if so, replace the text with <actinic:variable name="ProductDescription" />

                That worked here when copy / pasting the code from this forum didn't. P.S. I'm using Firefox.
                Norman - www.drillpine.biz
                Edinburgh, U K / Bitez, Turkey

                Comment


                  #9
                  This now seems to work, thank you so much Norman:

                  Code:
                  <p><actinic:block php="true" >	
                  $abc<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" /> = <<<EOD
                  <actinic:variable encoding="strip" name="ProductDescription" />
                  EOD;
                  $string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" /> =  addslashes($abc<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" />);
                  
                  $length = 190;
                      if (strlen($string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" />) > $length) {
                          //limit hit!        
                          $string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" /> = substr($string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" />,0,($length -3));
                              //stop on a word.
                              $string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" /> = substr($string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" />,0,strrpos($string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" />,' ')).'...';
                   $string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" /> = str_replace('\"', '', $string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" />);
                   $string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" /> = str_replace("\'", "'", $string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" />);
                      echo $string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" />;
                  }
                  else {
                  $string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" /> = str_replace('\"', '', $string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" />);
                   $string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" /> = str_replace("\'", "'", $string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" />);
                      echo $string<actinic:variable encoding="perl" name="EncodedProductAnchor" selectable="false" />;
                  }
                  	</actinic:block></p><br clear:both; />
                  Last edited by graphicz; 05-Jun-2015, 02:16 PM. Reason: Added an 'else' so brief descriptions do not get left out
                  Jonathan Chappell
                  Website Designer
                  SellerDeck Website Designer
                  Actinic to SellerDeck upgrades
                  Graphicz Limited - www.graphicz.co.uk

                  Comment


                    #10
                    Some redundant code and all those EncodedProductAnchors will cause SD's PHP to wastefully declare and assign a unique variable for every product. Try the much smaller and more readable:
                    Code:
                    <p><actinic:block php="true" >	
                    $length = 100;		// number of characters to trim to
                    $string = <<<EOD
                    <actinic:variable encoding="strip" name="ProductDescription" />
                    EOD;
                    if (strlen($string) > $length) {
                    	$string = substr($string, 0, $length);	// limit length
                    	$string = substr($string, 0, strrpos($string,' ')).'...';	// stop on a word.
                    	}
                    echo $string;
                    </actinic:block></p>
                    <br style="clear:both;" />
                    Norman - www.drillpine.biz
                    Edinburgh, U K / Bitez, Turkey

                    Comment


                      #11
                      Hi Norman and Jonathan,

                      This sounds like an excellent idea and I'd like to use the code as well.

                      Can you please tell me where I should put the code?

                      Arka Tribal Jewellery

                      Comment


                        #12
                        This code is intended for things like the Recent Products and Best Sellers, which don't usually display a description. Put it into the layouts associated with these items. Place it where you want the shortened description to show.

                        It won't work on Filtered pages or Search Results so is best avoided there.
                        Norman - www.drillpine.biz
                        Edinburgh, U K / Bitez, Turkey

                        Comment


                          #13
                          Thanks!

                          I have used it on the Product Summary Page, which shows is the section lists of products to tidy it up.
                          You click through to the main product to read the entire text desription. Works nicely.
                          Arka Tribal Jewellery

                          Comment

                          Working...
                          X