FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » use of substring in php
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
use of substring in php [message #174592] Mon, 20 June 2011 21:19 Go to next message
Co is currently offline  Co
Messages: 75
Registered: May 2011
Karma: 0
Member
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
Re: use of substring in php [message #174593 is a reply to message #174592] Mon, 20 June 2011 23:04 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
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
Re: use of substring in php [message #174594 is a reply to message #174592] Tue, 21 June 2011 00:40 Go to previous messageGo to next message
me is currently offline  me
Messages: 192
Registered: September 2010
Karma: 0
Senior Member
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
Re: use of substring in php [message #174595 is a reply to message #174592] Tue, 21 June 2011 00:41 Go to previous messageGo to next message
dougatmilmacdotcom is currently offline  dougatmilmacdotcom
Messages: 24
Registered: May 2011
Karma: 0
Junior Member
In article <27917623-6bef-416e-8ce3-856e9dd20bd9(at)v10g2000yqn(dot)googlegroups(dot)com>, Co <vonclausowitz(at)gmail(dot)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.
Re: use of substring in php [message #174596 is a reply to message #174595] Tue, 21 June 2011 01:16 Go to previous messageGo to next message
bobmct is currently offline  bobmct
Messages: 16
Registered: September 2010
Karma: 0
Junior Member
On Tue, 21 Jun 2011 00:41:31 GMT, dougatmilmacdotcom(at)example(dot)com (Doug
Miller) wrote:

> In article <27917623-6bef-416e-8ce3-856e9dd20bd9(at)v10g2000yqn(dot)googlegroups(dot)com>, Co <vonclausowitz(at)gmail(dot)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.
Re: use of substring in php [message #174597 is a reply to message #174592] Tue, 21 June 2011 09:04 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
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
Re: use of substring in php [message #174600 is a reply to message #174594] Tue, 21 June 2011 12:36 Go to previous message
Allodoxaphobia is currently offline  Allodoxaphobia
Messages: 21
Registered: September 2010
Karma: 0
Junior Member
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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Compiling PHP error
Next Topic: free computer ebooks updated daily
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 13:18:01 GMT 2024

Total time taken to generate the page: 0.02505 seconds