help with strip_tags and strlen

Discussion in 'PHP' started by adresaflorin, Jan 25, 2009.

  1. #1
    I'm trying to trim a html text to an exact lenght.
    I'm ussing strip_tags and strlen on the front page of jabulela.com but the text length is not always the same.
    How to trim to an exact lenght?
    do you know any snippets ?
     
    adresaflorin, Jan 25, 2009 IP
  2. ferdousx

    ferdousx Peon

    Messages:
    168
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    can you give your code. it may be modified to meet ur need.
     
    ferdousx, Jan 25, 2009 IP
  3. adresaflorin

    adresaflorin Peon

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    this is the code
    function truncate($string, $len, $wordsafe = FALSE, $dots = FALSE) {
    
    $string = strip_tags ($string, '<em><strong><b><u><i>');
    //$len = $len + (16 * substr_count($string, '<strong>')) + (8 * substr_count($string, '<em>')) + (6 * substr_count($string, '<b>'))+ (6 * substr_count($string, '<i>'));
    
      if (drupal_strlen($string) <= $len) {
        return $string;
      }
    
      if ($dots) {
        $len -= 4;
      }
    
      if ($wordsafe) {
        $string = drupal_substr($string, 0, $len + 1); // leave one more character
        if ($last_space = strrpos($string, ' ')) { // space exists AND is not on position 0
          $string = substr($string, 0, $last_space);
        }
        else {
          $string = drupal_substr($string, 0, $len);
        }
      }
      else {
        $string = drupal_substr($string, 0, $len);
      }
    
      if ($dots) {
        $string .= ' ...';
      }
    
      $string = _filter_htmlcorrector($string); // closes open tags
      
      return $string;
    }
    PHP:
    drupal_substr is
    function drupal_substr($text, $start, $length = NULL) {
      global $multibyte;
      if ($multibyte == UNICODE_MULTIBYTE) {
        return $length === NULL ? mb_substr($text, $start) : mb_substr($text, $start, $length);
      }
      else {
        $strlen = strlen($text);
        // Find the starting byte offset
        $bytes = 0;
        if ($start > 0) {
          // Count all the continuation bytes from the start until we have found
          // $start characters
          $bytes = -1; $chars = -1;
          while ($bytes < $strlen && $chars < $start) {
            $bytes++;
            $c = ord($text[$bytes]);
            if ($c < 0x80 || $c >= 0xC0) {
              $chars++;
            }
          }
        }
        else if ($start < 0) {
          // Count all the continuation bytes from the end until we have found
          // abs($start) characters
          $start = abs($start);
          $bytes = $strlen; $chars = 0;
          while ($bytes > 0 && $chars < $start) {
            $bytes--;
            $c = ord($text[$bytes]);
            if ($c < 0x80 || $c >= 0xC0) {
              $chars++;
            }
          }
        }
        $istart = $bytes;
    
        // Find the ending byte offset
        if ($length === NULL) {
          $bytes = $strlen - 1;
        }
        else if ($length > 0) {
          // Count all the continuation bytes from the starting index until we have
          // found $length + 1 characters. Then backtrack one byte.
          $bytes = $istart; $chars = 0;
          while ($bytes < $strlen && $chars < $length) {
            $bytes++;
            $c = ord($text[$bytes]);
            if ($c < 0x80 || $c >= 0xC0) {
              $chars++;
            }
          }
          $bytes--;
        }
        else if ($length < 0) {
          // Count all the continuation bytes from the end until we have found
          // abs($length) characters
          $length = abs($length);
          $bytes = $strlen - 1; $chars = 0;
          while ($bytes >= 0 && $chars < $length) {
            $c = ord($text[$bytes]);
            if ($c < 0x80 || $c >= 0xC0) {
              $chars++;
            }
            $bytes--;
          }
        }
        $iend = $bytes;
    
        return substr($text, $istart, max(0, $iend - $istart + 1));
      }
    }
    
    PHP:
     
    adresaflorin, Jan 25, 2009 IP
  4. NinjaWork

    NinjaWork Guest

    Messages:
    132
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    oh man...I love Drupal code :)
     
    NinjaWork, Jan 25, 2009 IP
  5. adresaflorin

    adresaflorin Peon

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks. I dropped wordpress for drupal, and I'm glad. :D
     
    adresaflorin, Jan 25, 2009 IP