I'm writing a blogging system and I'd like to be able to reduce the amount of info shown before someone is actually viewing the post. Like if they are on the page with all the posts only show say 256 letters at most. How can I go about doing this?
You can pull the descriptions from the database and use substr() to only grab a certain amount of characters. So something like: // .. SQL Stuff $description = $row['description']; // Only get 256 letters $description = substr($description, 0, 256); PHP: Hope that helps Glad you got your other issue worked out! Feel free to contact me if you have any more problems.
You can reduce the number of results returned from the database using LIMIT... this will return the first 10 results select title, content from tablename limit 0, 10 this will return results 11 through 20 select title, content from tablename limit 10, 10 or if you want shorter descriptions, you can use substr to truncate the string, like this $new_variable = substr($old_variable, 0, 255); the above code will put the first 255 characters of $old_variable into $new_variable
when doing that, i advice to put some number of words instead of number of chracters. This way you will not have broken words. and here is a function that will give you an "n" number of words from a string. function get_n_words($string, $wordsreturned) { $retval = $string; $array = explode(" ", $string); if (count($array)<=$wordsreturned) { $retval = $string; } else { array_splice($array, $wordsreturned); $retval = implode(" ", $array).""; } return $retval; } PHP: The whole string is returned if the number of words is less than the number of words in that string.
For faster query time use MySQL build-in function. So you don't have to use substr to cut your results. mysql_query("SELECT id,title,LEFT(post_content,256) as post FROM table") PHP: that way it will only pull 256 characters from post_content field and store in post
You can extract 256+max_word_len characters from DB, then use words boundary resize (this one is for UTF-8 input): function resize_by_words($text, $max_len=0, $more_str='...') { $max_word_len = 50; $max_repeated_str = 5; $text = utf8::trim($text); $text = preg_replace('/\s\s/ms', ' ', $text); $text = preg_replace('/(.)(\1){'.$max_repeated_str.',}/ms', '', $text); $text = preg_replace('/(.+)(\1\W*){'.$max_repeated_str.',}/ms', '', $text); if ($text=='.') $text = ''; $s = ''; for ($i=0, $n=0; $i<utf8::strlen($text); $i++) { if (!ctype_space($text[$i])) $n++; else $n = 0; if ($n>$max_word_len) {$s .= ' '; $n = 0;} $s .= $text[$i]; } $text = $s; if ($max_len>0 && utf8::strlen($text)>$max_len) { for ($i = $max_len; $i>=0 && !ctype_space($text[$i]); $i--) ; for (; $i>=0 && ctype_space($text[$i]); $i--) ; $text = utf8::substr($text, 0, $i+1).$more_str; } return $text; } купить квартиру в киеве продать квартиру в киеве ÑнÑть квартиру в киеве
Louis's method of doing is the most straight forward. That is to pull the whole database record, and then truncate it using the substring method.