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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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! |
|
|||
|
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/ |
|
|||
|
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 |