Forum Moderators: coopster

Message Too Old, No Replies

Show "x" amount of characters

example: show 500 characters from a variable.

         

PokeTech

12:28 am on Jun 1, 2008 (gmt 0)

10+ Year Member



Well I'm creating a blog system for my site and I'm trying to figure how I can display "x" amount characters.

I'm retrieving data from a mysql database, and lets say someone enters a few paragraphs of text. Lets say it has about 2000 characters. I want to be able to display only "x" amount of characters, lets say "x" equals 300. So It only displays 300 characters instead of 2000 characters.

If that makes any since please help.

dreamcatcher

6:59 am on Jun 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Two ways you can do it. Firstly, PHP has a built in function called substr [php.net]

echo substr($string,0,300);

However, if it was me I would let the database do the work with its own similar function:

$query = mysql_query("SELECT SUBSTRING('field',1,300) AS new_string FROM table...
$row = mysql_fetch_object($query);

echo $row->new_string;

Note that the pointer starts at 1 for mysql and not 0. More info here:
[dev.mysql.com...]

Good luck.

dc