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
Return to the default flat view Create a new topic Submit Reply
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 previous message
bobmct is currently offline  bobmct
Messages: 16
Registered: September 2010
Karma:
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.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Compiling PHP error
Next Topic: free computer ebooks updated daily
Goto Forum:
  

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

Current Time: Tue Nov 26 21:57:55 GMT 2024

Total time taken to generate the page: 0.04452 seconds