shortening a string

This is a discussion on shortening a string within the PHP Language forums, part of the PHP Programming Forums category; Hi I have a string: $string="aslkjdaslkdiwedweodij" When printing it out, I want it to be limited to ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-10-2005
asdfkajsdflkjsadlfkjoewqifoeiwjf@yahoo.com
 
Posts: n/a
Default shortening a string

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!

Reply With Quote
  #2 (permalink)  
Old 01-10-2005
Chris Hope
 
Posts: n/a
Default Re: shortening a string

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/
Reply With Quote
  #3 (permalink)  
Old 01-10-2005
Ronald Chaplin
 
Posts: n/a
Default Re: shortening a string

asdfkajsdflkjsadlfkjoewqifoeiwjf@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
Reply With Quote
  #4 (permalink)  
Old 01-11-2005
asdfkajsdflkjsadlfkjoewqifoeiwjf@yahoo.com
 
Posts: n/a
Default Re: shortening a string

Thanks guys! Works brilliantly

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:00 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0