Friday, September 11, 2009

how to limit the words/characters from db to show in PHP?

For PHP newbie!

This is a working code for you to limit the characters/ words you want to show.

Just change the characters limit for your need.
Enjoy!



                    $max_chars = 180;
                    $str_text = $my_string_from_the_database;
                    $no = count(explode(" ",$str_text));
                    $str_text =  trim(substr($str_text,0,$max_chars));
                                   
                                        $count = -1;
                                            for($j=0;$j < $no;$j++){
                                                //get the last character
                                                $new_text = substr($str_text,$count,1);


                                                //check the last character is the space or not
                                                if($new_text == " "){
                                                    $str_text = substr($str_text,0,$count);
                                                    echo $str_text;
                                                    break;
                                                }
                                            $count--;
                                            }
                               
                                    echo '…';

                   
                   
                    ?>


credit goes to the original developer but I forgot the resource.


မသိခ်င္းေတြနဲ႔ ရႈပ္ေနခဲ့တာ...

No comments:

Post a Comment