Re: shortening a string
From: Chris Hope (blackhole_at_electrictoolbox.com)
Date: 01/10/05
- Next message: Andy Hassall: "Re: php.net site is crashing Mozilla 1.7.5"
- Previous message: EDWARD CHARKOW: "Re: php://stdin limits?"
- In reply to: asdfkajsdflkjsadlfkjoewqifoeiwjf_at_yahoo.com: "shortening a string"
- Next in thread: Ronald Chaplin: "Re: shortening a string"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 10 Jan 2005 13:43:51 +1300
asdfkajsdflkjsadlfkjoewqifoeiwjf@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/
- Next message: Andy Hassall: "Re: php.net site is crashing Mozilla 1.7.5"
- Previous message: EDWARD CHARKOW: "Re: php://stdin limits?"
- In reply to: asdfkajsdflkjsadlfkjoewqifoeiwjf_at_yahoo.com: "shortening a string"
- Next in thread: Ronald Chaplin: "Re: shortening a string"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|