Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #2264 > unrolled thread
| Started by | Co <vonclausowitz@gmail.com> |
|---|---|
| First post | 2011-06-20 14:19 -0700 |
| Last post | 2011-06-21 11:04 +0200 |
| Articles | 7 — 7 participants |
Back to article view | Back to comp.lang.php
use of substring in php Co <vonclausowitz@gmail.com> - 2011-06-20 14:19 -0700
Re: use of substring in php Denis McMahon <denis.m.f.mcmahon@gmail.com> - 2011-06-20 23:04 +0000
Re: use of substring in php Bill B <me@privacy.net> - 2011-06-20 20:40 -0400
Re: use of substring in php Allodoxaphobia <knock_yourself_out@example.net> - 2011-06-21 12:36 +0000
Re: use of substring in php dougatmilmacdotcom@example.com (Doug Miller) - 2011-06-21 00:41 +0000
Re: use of substring in php bobmct <bobm3@worthless.info> - 2011-06-20 21:16 -0400
Re: use of substring in php Erwin Moller <Since_humans_read_this_I_am_spammed_too_much@spamyourself.com> - 2011-06-21 11:04 +0200
| From | Co <vonclausowitz@gmail.com> |
|---|---|
| Date | 2011-06-20 14:19 -0700 |
| Subject | use of substring in php |
| Message-ID | <27917623-6bef-416e-8ce3-856e9dd20bd9@v10g2000yqn.googlegroups.com> |
Hi All, I want to cut down the name of the user to 10 characters so it will fit to the picture of the user. At this moment I use $firstnameCut = substr($firstname, 0, 10); However sometimes the name is like: Johannes Paul. with my code it will show: Johannes P How can I change this so if the second or third name doesn't fit within the 10 characters it will not be shown at all? Regards Marco
[toc] | [next] | [standalone]
| From | Denis McMahon <denis.m.f.mcmahon@gmail.com> |
|---|---|
| Date | 2011-06-20 23:04 +0000 |
| Message-ID | <4dffd1e9$0$1089$bed64819@gradwell.net> |
| In reply to | #2264 |
On Mon, 20 Jun 2011 14:19:31 -0700, Co wrote: > Hi All, > > I want to cut down the name of the user to 10 characters so it will fit > to the picture of the user. > At this moment I use $firstnameCut = substr($firstname, 0, 10); However > sometimes the name is like: > > Johannes Paul. > with my code it will show: Johannes P > > How can I change this so if the second or third name doesn't fit within > the 10 characters > it will not be shown at all? a) Count back from the end of the string to find the location of the last space, and truncate the string to that point. b) split the name string into an array, and add bits of the string together with spaces until the next added bit would exceed your maximum length. There may be other ways. Rgds Denis McMahon
[toc] | [prev] | [next] | [standalone]
| From | Bill B <me@privacy.net> |
|---|---|
| Date | 2011-06-20 20:40 -0400 |
| Message-ID | <itopa4$hj3$1@dont-email.me> |
| In reply to | #2264 |
On 6/20/2011 5:19 PM, Co wrote: > Hi All, > > I want to cut down the name of the user to 10 characters so it will > fit to the picture of the user. > At this moment I use $firstnameCut = substr($firstname, 0, 10); > However sometimes the name is like: > > Johannes Paul. > with my code it will show: Johannes P > > How can I change this so if the second or third name doesn't fit > within the 10 characters > it will not be shown at all? This is not a PHP answer per se. Give some though to using the first X characters from the first name and the first Y characters from the last name. It may resolve some of your dilemma. It has a favorable side effect of producing a decently unique set of characters (most of the time). Bill B
[toc] | [prev] | [next] | [standalone]
| From | Allodoxaphobia <knock_yourself_out@example.net> |
|---|---|
| Date | 2011-06-21 12:36 +0000 |
| Message-ID | <slrnj01436.snb.knock_yourself_out@shell.config.com> |
| In reply to | #2266 |
On Mon, 20 Jun 2011 20:40:40 -0400, Bill B wrote: > On 6/20/2011 5:19 PM, Co wrote: >> >> I want to cut down the name of the user to 10 characters so it will >> fit to the picture of the user. >> At this moment I use $firstnameCut = substr($firstname, 0, 10); >> However sometimes the name is like: >> >> Johannes Paul. >> with my code it will show: Johannes P >> >> How can I change this so if the second or third name doesn't fit >> within the 10 characters >> it will not be shown at all? > > This is not a PHP answer per se. Give some though to using the first X > characters from the first name and the first Y characters from the last > name. It may resolve some of your dilemma. > It has a favorable side > effect of producing a decently unique set of characters (most of the time). And, an 'interesting' side effect of sometimes producing fairly vulgar character strings... Jonesy
[toc] | [prev] | [next] | [standalone]
| From | dougatmilmacdotcom@example.com (Doug Miller) |
|---|---|
| Date | 2011-06-21 00:41 +0000 |
| Message-ID | <itopc9$j4l$1@dont-email.me> |
| In reply to | #2264 |
In article <27917623-6bef-416e-8ce3-856e9dd20bd9@v10g2000yqn.googlegroups.com>, Co <vonclausowitz@gmail.com> wrote: >Hi All, > >I want to cut down the name of the user to 10 characters so it will >fit to the picture of the user. >At this moment I use $firstnameCut = substr($firstname, 0, 10); >However sometimes the name is like: > >Johannes Paul. >with my code it will show: Johannes P > >How can I change this so if the second or third name doesn't fit >within the 10 characters >it will not be shown at all? What will you do if the *first* name doesn't fit within 10 characters? In English-speaking countries, "Christopher" (11 characters) is a fairly common given name for men. I have a Polish friend whose given name is Wlodzimierz (also 11 characters). In some languages (e.g. Thai or Malayalam) names can be *much* longer than that. Perhaps this will produce what you want: a) if the 11th character is a space, truncate at 10 characters and quit b) if there are no spaces in the first 10 characters, truncate at 10 and quit c) if neither a) nor b) is true, search backward from the 10th character and truncate at the first space you find.
[toc] | [prev] | [next] | [standalone]
| From | bobmct <bobm3@worthless.info> |
|---|---|
| Date | 2011-06-20 21:16 -0400 |
| Message-ID | <h0svv6d6q95er86ea4ov3m9r0camvt8mo5@4ax.com> |
| In reply to | #2267 |
On Tue, 21 Jun 2011 00:41:31 GMT, dougatmilmacdotcom@example.com (Doug
Miller) wrote:
>In article <27917623-6bef-416e-8ce3-856e9dd20bd9@v10g2000yqn.googlegroups.com>, Co <vonclausowitz@gmail.com> wrote:
>>Hi All,
>>
>>I want to cut down the name of the user to 10 characters so it will
>>fit to the picture of the user.
>>At this moment I use $firstnameCut = substr($firstname, 0, 10);
>>However sometimes the name is like:
>>
>>Johannes Paul.
>>with my code it will show: Johannes P
>>
>>How can I change this so if the second or third name doesn't fit
>>within the 10 characters
>>it will not be shown at all?
>
>What will you do if the *first* name doesn't fit within 10 characters? In
>English-speaking countries, "Christopher" (11 characters) is a fairly common
>given name for men. I have a Polish friend whose given name is Wlodzimierz
>(also 11 characters). In some languages (e.g. Thai or Malayalam) names can be
>*much* longer than that.
>
>Perhaps this will produce what you want:
>
>a) if the 11th character is a space, truncate at 10 characters and quit
>b) if there are no spaces in the first 10 characters, truncate at 10 and quit
>c) if neither a) nor b) is true, search backward from the 10th character and
>truncate at the first space you find.
How about somthing like this:
$x = strpos($Name, " ");
if ($x > 10) { $x = 10; }
$FirstName = substr($Name, 0, $x);
This might accomplish close to what you are looking for. Tweat this
and it should work OK for you.
Good luck.
[toc] | [prev] | [next] | [standalone]
| From | Erwin Moller <Since_humans_read_this_I_am_spammed_too_much@spamyourself.com> |
|---|---|
| Date | 2011-06-21 11:04 +0200 |
| Message-ID | <4e005e76$0$49174$e4fe514c@news.xs4all.nl> |
| In reply to | #2264 |
On 6/20/2011 11:19 PM, Co wrote: > Hi All, > > I want to cut down the name of the user to 10 characters so it will > fit to the picture of the user. > At this moment I use $firstnameCut = substr($firstname, 0, 10); > However sometimes the name is like: > > Johannes Paul. > with my code it will show: Johannes P > > How can I change this so if the second or third name doesn't fit > within the 10 characters > it will not be shown at all? > > Regards > Marco Hello Marco, I like using ... to show the visitor that something is cutted away. eg: Marco --> Marco Johannes Paul -> Johannes... But that works better for longer strings. If you only want 10 characters, the 3 ... take up too much space I expect. Also, make sure you give your image a title="Johannes Paul" and alt="Johannes Paul" so everybody can easily find the full name by hovering over the image. Regards, Erwin Moller -- "That which can be asserted without evidence, can be dismissed without evidence." -- Christopher Hitchens
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web