homepage Welcome to WebmasterWorld Guest from 54.221.175.46
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How to lower exploded text and increase first letter?
toplisek




msg:4311867
 2:42 pm on May 13, 2011 (gmt 0)

I have code like:
foreach($var as $i=>$element) {
$var[$i] = ucfirst($element);
}

Issue is that exploded text is all the time CAPS like
TEXT

When I try to ucfirst it will be not Text as all are CAPS.

PHP:
Returns a string with the first character of str capitalized, if that character is alphabetic.


If I try $var[$i] = mb_strtolower($element); it will be all lower.

How to set in one line mb_strtolower and the first character capitalized like Text?

 

mbabuskov




msg:4311880
 2:55 pm on May 13, 2011 (gmt 0)

Combine both functions: ucfirst(strtolower($element))

For Unicode version use mb_convert_case(mb_strtolower($str), MB_CASE_TITLE)

Gibisan




msg:4311881
 2:56 pm on May 13, 2011 (gmt 0)

Is this what you want:

ucfirst(strtolower($element))

toplisek




msg:4311901
 3:22 pm on May 13, 2011 (gmt 0)

ucfirst(strtolower($element)) will not other elements increase first letter and does not support UTF.
First element is correct.

How to do in both case?

mbabuskov




msg:4311937
 4:13 pm on May 13, 2011 (gmt 0)

See my reply above. mb_convert_case supports UTF-8

toplisek




msg:4312068
 7:29 pm on May 13, 2011 (gmt 0)

I have tested many possibilities.

It works $var[$i] = mb_convert_case(mb_strtolower($element), MB_CASE_TITLE);

But still is issue UTF-8 encoding. How to work some special characters like č.
It shows me �

I worked some other function mb_strtolower($variable, 'UTF-8'))

How to set UTF-8 inside:
$var[$i] = mb_convert_case(mb_strtolower($element), MB_CASE_TITLE);

mbabuskov




msg:4312296
 7:22 am on May 14, 2011 (gmt 0)

$var[$i] = mb_convert_case(mb_strtolower($element, 'UTF-8'), MB_CASE_TITLE, 'UTF-8');

toplisek




msg:4312301
 7:45 am on May 14, 2011 (gmt 0)

You are right. Thanks.
It works: $var[$i] = mb_convert_case(mb_strtolower($element, 'UTF-8'), MB_CASE_TITLE, 'UTF-8');

I have last issue. Each array values consist of two-three words like
WORLD1 WORLD2

One array: WORLD1 WORLD1
It will return your script the following inside ONE array:
World1 World2

I like to make
World1 world1

It seems to modify ALL words to CAPS for the first letter.
I like to make the first world just first word. But as usual next array in the same change :World2 world2

Matthew1980




msg:4312302
 7:45 am on May 14, 2011 (gmt 0)

Hello everyone,

I'm tired so may have missed the point here a little, but surely this [uk2.php.net] would be the function to use.

Handy to have this when manipulating text submitted by a user...

Cheers,
MRb

coopster




msg:4313043
 12:46 pm on May 16, 2011 (gmt 0)

It seems to modify ALL words to CAPS for the first letter.


If you want ONLY the first word in the entire array you could either flatten the string again after your loop and apply the uppercase ...
foreach($var as $i=>$element) { 
mb_convert_case($var[$i], MB_CASE_TITLE, 'UTF-8');
}
$var = mb_convert_case(implode(' ', $var), MB_CASE_TITLE, 'UTF-8');

... or check within the loop if it is the first array element and apply the uppercase to just that index value:
foreach($var as $i=>$element) { 
$var[$i] = mb_strtolower($element, 'UTF-8');
if (!i) {
$var[$i] = mb_convert_case($var[$i], MB_CASE_TITLE, 'UTF-8');
}
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved