How can I combine strip_tags and substr ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hodja451
    New Member
    • Jan 2010
    • 2

    How can I combine strip_tags and substr ?

    I want to strip tags and return the first x characters. Every combo I try fails.

    The code below is where I'm at. It works perfectly stripping the tags, it fails when I try to add substr to limit the length. Not that it should matter, but this is within an Oscommerce setup.


    Code:
    else {
                  $lc_text = '&nbsp;<big><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</big></a>&nbsp;<br><br>' . strip_tags($listing['products_description'], '<br>') . '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . '<strong><font color="#36C426">(...more)</font></strong></big></a>' . '&nbsp;';
                }
    Any help would be greatly appreciated.

    Thanks,
    Walter
    Last edited by Dormilich; Jan 24 '10, 10:15 PM. Reason: Please use [code] tags when posting code
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    You are not using substr at all in that code...

    However...

    Code:
    function buildBriefDescription($description, $length = 100) {
        if (strlen($description) > $length) {
            $description = strip_tags($description);
            return substr($description, 0, $length) . '...';
        }
        return $description;
    }

    Comment

    • hodja451
      New Member
      • Jan 2010
      • 2

      #3
      Thanks for the reply!

      I wasn't using substr, because however I placed it, I broke the code, I needed to know how to place it.

      I worked on this for hours before asking, then I found the solution 5 minutes after asking.

      I just changed

      strip_tags($lis ting['products_descr iption'], '<br>')

      to

      substr(strip_ta gs($listing['products_descr iption'], '<br>'), 0, 100)

      Comment

      Working...