Jump to content

[SOLVED] capitalize every other letter..


poofried

Recommended Posts

<?php
$string = "loremipsum";
$string = strtolower($string);
$i = 0;
$c = 2;
$alpha = 'abcdefghijklmnopqrstuvwxyz';
$s = '';
while ($l = substr($string,$i++,1)) {
	if (strpos($alpha,$l) !== false) {
		if ($c == 2) { $l = strtoupper($l); $c = 0; }
		$c++;
	}
	$s .= $l;
}
echo $s;
?>

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

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!

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

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.

russell

 

grate stuff

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

 

where you been hiding lol

 

how the hell this work, how the hell is the pointer moved every two characters...

<?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

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;

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'].

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.