By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,204 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,204 IT Pros & Developers. It's quick & easy.

shortening a string

P: n/a
Hi

I have a string:

$string="aslkjdaslkdiwedweodij"

When printing it out, I want it to be limited to 10 characters followed
by "..."

Result:

"asjdksledo..."

I only want this to happen when the string is longer than 10
characters.

How can this be done in php? I know how to do it in Perl through reg
exp.. :/

Thanks!

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
as******************************@yahoo.com wrote:
I have a string:

$string="aslkjdaslkdiwedweodij"

When printing it out, I want it to be limited to 10 characters
followed by "..."

Result:

"asjdksledo..."

I only want this to happen when the string is longer than 10
characters.

How can this be done in php? I know how to do it in Perl through reg
exp.. :/


How about something like:

if(strlen($string) > 10) {
print substr($string, 0, 10) . '...';
}
else {
print $string;
}

It could also be done on one line like so:

strlen($string) > 10 ? print substr($string, 0, 10) . '...' : print
$string;

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2

P: n/a
as******************************@yahoo.com wrote:
How can this be done in php? I know how to do it in Perl through reg
exp.. :/


You can use reg expressions in PHP as well :)
http://us4.php.net/manual/en/function.preg-match.php

This uses Perl style expression matching

HTH

Ron Chaplin
Jul 17 '05 #3

P: n/a
Thanks guys! Works brilliantly

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.