Page 1 of 1

Truncate an Echo

Posted: Tue Aug 31, 2004 10:20 pm
by AliasBDI
I have an echo from a search query. The code reads:

Code: Select all

<?php echo $row_recordLIST['content']; ?>
The result is way too long (like 5,000 characters of text). I want to shorten it so that it only shows like 300 characters and follows them with a '...' to let the view know that there is more. I believe this is called truncating, but I cannot figure out how to do it.

Posted: Tue Aug 31, 2004 10:25 pm
by timvw
substr...... php has it, and i think mysql has it too (could be called substring there)

Thanks.

Posted: Wed Sep 01, 2004 7:08 am
by AliasBDI
Works with 'substr'. Here is my new code:

Code: Select all

<?php $content = $row_recordLIST['content']; $content = substr($content, 0, 1000); echo $content . "..."; ?>
The truncating happens in "substr($variable, 0, 1000)". The first number is for the beginning of the line. The second is the cut off the end of the line. So it is removing -1 and back (which is nothing) and 1001 and forward. Basically cutting the ends off the result. I added the "..." just to let the user know that there was more.

Thanks for your help.

Posted: Wed Sep 01, 2004 8:19 am
by timvw
i mentionned that mysql has substring too, because it would be useless to transfer +10k characters, if you only need 50 or so...


Say, i want to show the 20 first words of a column named content

Code: Select all

SELECT SUBSTRING_INDEX(content,' ',20) AS content FROM table;