Welcome to WebmasterWorld Guest from 50.17.74.162

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to lower exploded text and increase first letter?

     

toplisek

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

WebmasterWorld Senior Member 5+ Year Member



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

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

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

10+ Year Member



Is this what you want:

ucfirst(strtolower($element))

toplisek

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

WebmasterWorld Senior Member 5+ Year Member



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

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



See my reply above. mb_convert_case supports UTF-8

toplisek

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

WebmasterWorld Senior Member 5+ Year Member



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

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

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

WebmasterWorld Senior Member 5+ Year Member



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

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

WebmasterWorld Senior Member 5+ Year Member



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

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

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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');
}
}