How to limit words?

Discussion in 'PHP' started by caspermz, Jun 28, 2009.

  1. #1
    Hello im using this code for inserting news on my site from how i can limit the words that apear ? this is my actual code.

    mysql_select_db($db,$conn) or die("Unable to select to MySQL database");
     
    		$query = "select  wp_posts.ID, wp_posts.post_author, wp_posts.post_date, wp_posts.post_date_gmt, wp_posts.post_content as post_content, wp_posts.post_title as post_title, wp_posts.post_category, wp_posts.post_excerpt, wp_posts.post_status as post_status, wp_posts.comment_status, wp_posts.ping_status, wp_posts.post_password, wp_posts.post_name, wp_posts.to_ping, wp_posts.pinged, wp_posts.post_modified, wp_posts.post_modified_gmt, wp_posts.post_content_filtered, wp_posts.post_parent, wp_posts.guid, wp_posts.menu_order, wp_posts.post_type, wp_posts.post_mime_type, wp_posts.comment_count from wp_posts where wp_posts.post_type = 'post' order by wp_posts.post_date desc limit 2"; 
    						
    		$results = mysql_query($query); 		
    		
    		if(!$results) 
    		{
    			echo 'query failed'; 
    		}
    		else 
    		{
    			while($row=mysql_fetch_assoc($results))
    			{
    				
    				
    
    				
    				$tittle = $row['post_title'];  
    			        $content = $row['post_content'];  
    
    
    				
    				print("<p style='font-family:Trebuchet MS, sans-serif; font-size:13px; font-weight:bold; color:#f33858; margin-top:0px;'>"); 
    			858;' href='#'>$tittle</a>
    </p>");  
    				print("<div style='font-family:Arial, Helvetica, sans-serif; font-size:11px; line-height:130%; color:#757575 !important; margin-top:9px;'>$content</div>");
    			 
    			}
    		}		
    	
    
    
    
    ?>
    PHP:

     
    caspermz, Jun 28, 2009 IP
  2. burdock

    burdock Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There are many ways to do this; however a very quick and dirty way is to explode the string into an array. After the following line:

    $content = $row['post_content'];
    PHP:
    Add:

    $wordLimit = 50; // How many words you want to display
    $end = '...'; // Added to the end of your content
    
    $wordArray = explode(' ', $content);
    $string = '';
    for($i=0, $i<$wordLimit, $i++) {
      $string .= $wordArray[$i].' ';
    }
    $content = rtrim($content).$end;
    PHP:
    (Sorry, I am unable to test the above at the moment, but it should work just fine.)


    The above code is a simple way to implement what you are looking for, and by no means optimized. Also, if the content you are outputting is likely to contain HTML tags, let me know, and we can address that also.
     
    burdock, Jun 28, 2009 IP
  3. JDevereux

    JDevereux Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I use this function. Obviously you can dump the ellipsis from the end if you want to.

    function wordlimit($string, $length = 50, $ellipsis = "...") 
    { 
       $words = explode(' ', $string); 
       if (count($words) > $length) 
           return implode(' ', array_slice($words, 0, $length)) . $ellipsis; 
       else 
           return $string; 
    }
    PHP:
     
    JDevereux, Jun 28, 2009 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    <?php
    
    $str = 'Morbi nisl tortor, consectetur vitae laoreet eu, lobortis id ipsum. Integer scelerisque blandit pulvinar. Nam tempus mi eget nunc laoreet venenatis. Proin viverra, erat at accumsan tincidunt, ante mi cursus elit, non
    congue mauris dolor ac elit. Maecenas mollis nisl a sem semper ornare. Integer nunc purus, dapibus nec dignissim sed, dictum eget leo. Etiam in mi ut erat pretium fringilla sed';
    
    function get_words(&$str, $max_words = 1)
    {
    	$current_space	= 0;
    	$last_space		= strrpos($str, ' ');
    	$words			= 0;
    	$max_words		= max(1, (int)$max_words);
    
    	while($current_space < $last_space && $words < $max_words)
    	{
    		$current_space = strpos($str, ' ', ++$current_space);
    		$words++;
    	}
    
    	return trim($words < $max_words ? $str : substr($str, 0, $current_space), ' ,.?!&');
    
    }
    
    echo "\n" . get_words($str, 9) . "\n";
    
    ?>
    PHP:
     
    joebert, Jun 29, 2009 IP
  5. jessicaloreen

    jessicaloreen Member

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    php Code:
    <?php

    $str = 'Morbi nisl tortor, consectetur vitae laoreet eu, lobortis id ipsum. Integer scelerisque blandit pulvinar. Nam tempus mi eget nunc laoreet venenatis. Proin viverra, erat at accumsan tincidunt, ante mi cursus elit, non
    congue mauris dolor ac elit. Maecenas mollis nisl a sem semper ornare. Integer nunc purus, dapibus nec dignissim sed, dictum eget leo. Etiam in mi ut erat pretium fringilla sed';

    function get_words(&$str, $max_words = 1)
    {
    $current_space = 0;
    $last_space = strrpos($str, ' ');
    $words = 0;
    $max_words = max(1, (int)$max_words);

    while($current_space < $last_space && $words < $max_words)
    {
    $current_space = strpos($str, ' ', ++$current_space);
    $words++;
    }

    return trim($words < $max_words ? $str : substr($str, 0, $current_space), ' ,.?!&');

    }

    echo "\n" . get_words($str, 9) . "\n";

    ?>
     
    jessicaloreen, Jun 29, 2009 IP