Tuesday, February 23, 2010

php get intro from text function

i modified the function to what it is now because it took a long time when used in a loop.

function determineIntroFromString( $string, $length=null ){
      
        $length = ( empty( $length ) || $length < 350 ) ? 400 : $length;
      
        $sentences = explode( ".", $string );
          $intro = "";
         
          $index = 0;
          $string_length = 0;
        
          foreach ( $sentences as $sentence ){
            
              //echo " ---  ".$sentence;
              //echo strlen($sentence);exit;
            
              if( $string_length <= $length ){
                  $intro .= $sentence;
                  $string_length = strlen( $intro );
                  //echo " -- ".$string_length;
                  unset( $sentence );
              } else {
                  break;
              }
              //unset( $sentence );
          }
        
      /*    while (  strlen( $intro ) <= $length ){
              $intro .= $sentences[$index];
              $index++;
          }*/
        
          $intro .= ".";
        
        return $intro;
    }

No comments: