469,315 Members | 1,602 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

displaying first 50 characters of a mysql text field?

Hi all

I have been searching for hours on the php website for a function that
will allow me to display just the first 50 chars of a mysql text field
in a results table, but i'm having no luck. I need it to create a
results page for a site i'm creating in php. Can anyone help with this
problem?

Kind regards

Marc

Oct 23 '05 #1
4 8060
phpuser32423 wrote:
Hi all

I have been searching for hours on the php website for a function that
will allow me to display just the first 50 chars of a mysql text field
in a results table, but i'm having no luck. I need it to create a
results page for a site i'm creating in php. Can anyone help with this
problem?

Kind regards

Marc


$str50 = substr($mystring, 0, 50);

-david-

Oct 23 '05 #2
I noticed that Message-ID: <0d***********@fe24.usenetserver.com> from
David Haynes contained the following:

$str50 = substr($mystring, 0, 50);


I use this method in a function and if the text is truncated the
function adds '...more' at the end. The function also takes a url so
that the word 'more' can be a link to an expanded version. $length is
obviously the amount you to which you want to truncate. The function
also 'steps back' to avoid truncating in the middle of a word.

<?php
function truncate($value,$length,$url){
if(strlen($value)>$length){
$value=substr($value,0,$length);
$n=0;
while(substr($value,-1)!=chr(32)){
$n++;
$value=substr($value,0,$length-$n);
}
$value=$value." ...<a href='$url'><em>more</em></a>";
}
return $value;
}
//usage
echo truncate("the quick brown fox jumps over the lazy
dog",15,"http:www.example.com?id=someid");
?>
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Oct 23 '05 #3
thanks guys thats a great help

Oct 23 '05 #4
phpuser32423 wrote:
Hi all

I have been searching for hours on the php website for a function that
will allow me to display just the first 50 chars of a mysql text field
in a results table, but i'm having no luck. I need it to create a
results page for a site i'm creating in php. Can anyone help with this
problem?

Kind regards

Marc


If you only need the first 50 chars for that field then you can get
MySQL to do the work for you ...

MySQL - Substring: Extracting part of a string

SELECT name, SUBSTRING(name,0,50) FROM cia WHERE region = 'Asia'

MySQL docs... http://dev.mysql.com/doc/refman/5.0/...functions.html

Craig
Oct 24 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Mosher | last post: by
4 posts views Thread by Kivanc Toker | last post: by
2 posts views Thread by donpro | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.