Click to See Complete Forum and Search --> : How to return a part of a string...


stanleyns
05-22-2006, 08:19 PM
hi guys..
i need help in returning a part of a string if the string is too long (more than 10 characters).. and will give a (...) on the end of the sentence..
for example:
the sentence "What is it now" will return "What is it..."

edit: i know how to substr and i managed to make it display "What is it" but without the (...)
what i'd like to know is, how do i make it so that the (...) will appear...

any kind of help or suggestions is very much appreciated..
thank you..

aaronbdavis
05-22-2006, 08:48 PM
use the dot (concatenation) operator if (10 < strlen($string)
{
echo substr($string, 0, 10) . '...';
} else
{
echo $string;
}

stanleyns
05-22-2006, 08:50 PM
thanks alot ABD :)
i kinda get it to work..

NogDog
05-22-2006, 09:27 PM
You don't really need the length check: substr() will stop at the end of the string:

echo substr($string, 0, 10); // this will work fine regardless of length of $string