Jump to content

[SOLVED] capitalize every other letter..


poofried

Recommended Posts

Not sure what that is, RussellReal. It doesn't even output anything, maybe you're misreading what he wants?

 

Try:

 

<?php
$str = 'loremipsum';
$split = str_split($str);
foreach($split as $key => &$letter)
if(!($key % 2))
	$letter = strtoupper($letter);
echo implode($split);

 

Or

 

<?php
$str = 'loremipsum';
for($i = 0;$i < strlen($str);$i++)
if(!($i % 2))
	$str{$i} = strtoupper($str{$i});
echo $str;

Just having some fun :P

Link to comment
Share on other sites

Not sure what that is, RussellReal. It doesn't even output anything, maybe you're misreading what he wants?

 

Try:

 

<?php
$str = 'loremipsum';
$split = str_split($str);
foreach($split as $key => &$letter)
if(!($key % 2))
	$letter = strtoupper($letter);
echo implode($split);

 

thanks, this did it!

Link to comment
Share on other sites

Not sure what that is, RussellReal. It doesn't even output anything, maybe you're misreading what he wants?

 

Try:

 

<?php
$str = 'loremipsum';
$split = str_split($str);
foreach($split as $key => &$letter)
if(!($key % 2))
	$letter = strtoupper($letter);
echo implode($split);

 

Or

 

<?php
$str = 'loremipsum';
for($i = 0;$i < strlen($str);$i++)
if(!($i % 2))
	$str{$i} = strtoupper($str{$i});
echo $str;

Just having some fun :P

 

mine DOES work.. its tested (by me) to work, also, yours looks better so he should stick with yours imo :) Just don't talk about my code if you haven't tried it. Thanx

Link to comment
Share on other sites

Not sure what that is, RussellReal. It doesn't even output anything, maybe you're misreading what he wants?

 

Try:

 

<?php
$str = 'loremipsum';
$split = str_split($str);
foreach($split as $key => &$letter)
if(!($key % 2))
	$letter = strtoupper($letter);
echo implode($split);

 

Or

 

<?php
$str = 'loremipsum';
for($i = 0;$i < strlen($str);$i++)
if(!($i % 2))
	$str{$i} = strtoupper($str{$i});
echo $str;

Just having some fun :P

 

mine DOES work.. its tested (by me) to work, also, yours looks better so he should stick with yours imo :) Just don't talk about my code if you haven't tried it. Thanx

I did try it, but I tried it before you edited it, so that's why it does work. After you edited it to fix it, it does work. I believe the only problem was that at the end you were echoing the wrong variable.

Link to comment
Share on other sites

<?php

$str = 'loremipsum';

for($i = 0;$i < strlen($str);$i++)

if(!($i % 2))

$str{$i} = strtoupper($str{$i});

echo $str;

?>

 

{N} is basically gonna get that offset by 1 which is Nth character in the string.. if (!($i % 2)) which ofcourse is a modulus which checks if there is a remainder after division if $i is 3 which is the loop its in, than it will have a remainder of 1 and thus will be true.. ! is the negator which means it is only looking for false, 0 is interpreted as false, which all together means if there is no remainder in the loop number after division of 2 than uppercase it then set it back that way in the variable which gets the character at said offset..

 

I also just realised his script does not take into consideration spaces and numbers and other non-alphanumeric characters like my script does, for example

 

.l.o.r.e.m.i.p.s.u.m

 

with his script will return

 

.l.o.r.e.m.i.p.s.u.m

 

where as mine would return:

 

.L.o.R.e.M.i.P.s.U.m

 

or something more relevant:

 

Hey... My name is Russell

 

mine lowercases everything which in his script does not either, aswell.

 

also.. mine would return

 

HeY... mY nAmE iS rUsSeLl

 

and his would return

 

HeY... mY NaMe iS RuSsElL

Link to comment
Share on other sites

True, mine doesn't account for that. GG.

 

One small tip though, you don't need to create a string containing a-z then using strpos(), you can just use ctype_alpha()

 

Edit: Accounting for what RussellReal pointed out, here's my new version:

 

$str = '.l.o.r.e.m.i.p.s.u.m';
for($i = 0, $cap = true;$i < strlen($str);$i++)
{
if(!ctype_alpha($str{$i}))
	continue;
$str{$i} = ($cap) ? strtoupper($str{$i}) : $str{$i};
$cap = !$cap;
}
echo $str;

Link to comment
Share on other sites

Please do note that using curly brackets { } in a string is considered deprecated.

 

Note: Strings may also be accessed using braces' date=' as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 5.3.0. Use square brackets instead, such as $str[42'].
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.