TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Limit number of Words when fetching. (http://www.talkphp.com/advanced-php-programming/4821-limit-number-words-when-fetching.html)

russellharrower 08-07-2009 04:04 AM

Limit number of Words when fetching.
 
Hi,
I am using the following code to fetch data from the database. However while all the code works, one thing I can not figure out is how to make it so that for e.g

Say I have have the following text

Quote:

Editor’s note: The following report comes from Don Dodge, who blogs at Don Dodge on The Next Big Thing and is a business development executive for Microsoft. TechStarsis a startup incubator that selects 10 teams and provides funding of $18,000 per team, as well as free office space, operational support, and mentoring from investors, entrepreneurs and business leaders.
However when a user does a search for "business development" instead of showing from the word Editor's I want to show it from the word The Next Big Thing.

Is there any way to do that?

here is my code
Code:

<?php
        $position=300; // Define how many character you want to display.

 
                while($row = mysql_fetch_array($result))
                {
       
        $post = substr($message,$position,1); // Find what is the last character displaying. We find it by getting only last one character from your display message.

        if($post !=" "){ // In this step, if last character is not " "(space) do this step .

        // Find until we found that last character is " "(space)
        // by $position+1 (14+1=15, 15+1=16 until we found " "(space) that mean character 20)
        while($post !=" "){
        $i=1;
        $position=$position+$i;

        $post = substr($row['body'],$position,1);
        }

        }

        $post = substr($row['body'],0,$position); // Display your message
        echo $post;
        echo "...";
        echo "<br/><br/>";
        }
        ?>


russellharrower 08-09-2009 04:08 AM

This is my latest code, and it works great, however if the db has in its row something like the following
Quote:

Today Yahoo and Microsoft came to a deal, were Microsoft would power Yahoo search.

In return Yahoo will....
it seems this script stops at power Yahoo Search. instead of yahoo will... I believe it is due to the line space.

Code:

function cut_it($str,$len=400) { //default is 20, but you can switch to anything you want here on when calling it
if (strlen($str)<=$len) { return $str; }
$str = wordwrap($str,$len); //defaults to the behaviour twey setup
$str = explode("\n",$str,2); //lazy-- you could use substr/strpos, etc
$str = $str[0];
if ($str>$len) {
$str = substr($str,0,$len-3).'...';
}
return $str;
}

               
                  while($row = mysql_fetch_array($result))
                                {
                $title = $row['title'];
       
                print $title;
                echo "<br/>";
                $body = cut_it($row['body']);
                print $body;
                echo "<br/><br/>";
                }

If you can help that would be great of you...


All times are GMT. The time now is 09:38 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0