473,394 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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!

Jul 17 '05 #1
3 2043
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
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
Thanks guys! Works brilliantly

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: kaptain kernel | last post by:
how does one shorten an array based on a key value. Say I have this: array("Car1"=>"Porsche","Car2"=>"Merc","Car3"=>"Ford","Car4"=>"Toyota"); and I want to pop "Car2" out of the array,...
16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
4
by: Dariusz | last post by:
I have a guestbook on one of my sites where the entires are formatted to be x pixles in width. Now the text is input and output from a "blob" field from a database... but when people input long...
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.