I am barely a hack when it comes to PHP so please forgive my terminology: I am getting a row from a MySql Database and displaying several fields. One of the fields is text and I would like to display only the first 50 characters in the field. How in the world can I do this? I am currently using this: But it displays the whole text. I just want the first 50 characters or so. Thanks for the help.
Personally I would do it on a sql level as long as you dont need all the data at any time in the script. It reduces a little of the processing time (usually) and also eliminates the extra data from hitting php at all. $query = "SELECT SUBSTR(title,0,50), other columns... FROM database ...."; PHP:
You can use LEFT instead of SUBSTR $query = "SELECT LEFT(text,50) FROM table"; You can also add a string to the end to signify that you cut off the text and there is actually more. Something like '...' This is what I am selecting from my table. I only... $query = "SELECT CONCAT(LEFT(text,50),'...') FROM table"; Heres a whole list of MySQL 5.0 string functions (LEFT & CONCAT work in 4.1): hxxp://dev.mysql.com/doc/refman/5.0/en/string-functions.html hxxp://dev.mysql.com/doc/refman/4.1/en/string-functions.html