poofried Posted September 29, 2009 Share Posted September 29, 2009 how can i capitalize every other letter in an input string via php.. like if i insert loremipsum into the form it should output LoReMiPsUm ? Quote Link to comment https://forums.phpfreaks.com/topic/175880-solved-capitalize-every-other-letter/ Share on other sites More sharing options...
poofried Posted September 29, 2009 Author Share Posted September 29, 2009 anyone? i really need this feature for security reasons. i just have no idea how to get it started. i know its something with ucwords or strtoupper maybe? not sure. Quote Link to comment https://forums.phpfreaks.com/topic/175880-solved-capitalize-every-other-letter/#findComment-926711 Share on other sites More sharing options...
RussellReal Posted September 29, 2009 Share Posted September 29, 2009 <?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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/175880-solved-capitalize-every-other-letter/#findComment-926714 Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175880-solved-capitalize-every-other-letter/#findComment-926716 Share on other sites More sharing options...
poofried Posted September 29, 2009 Author Share Posted September 29, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/175880-solved-capitalize-every-other-letter/#findComment-926717 Share on other sites More sharing options...
RussellReal Posted September 29, 2009 Share Posted September 29, 2009 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 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 Quote Link to comment https://forums.phpfreaks.com/topic/175880-solved-capitalize-every-other-letter/#findComment-926781 Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 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 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. Quote Link to comment https://forums.phpfreaks.com/topic/175880-solved-capitalize-every-other-letter/#findComment-926783 Share on other sites More sharing options...
redarrow Posted September 29, 2009 Share Posted September 29, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/175880-solved-capitalize-every-other-letter/#findComment-926785 Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 Lol, I'm an idiot. Just realized it could've been done like this also: <?php $str = 'loremipsum'; for($i = 0;$i < strlen($str);$i+=2) $str{$i} = strtoupper($str{$i}); echo $str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/175880-solved-capitalize-every-other-letter/#findComment-926788 Share on other sites More sharing options...
RussellReal Posted September 29, 2009 Share Posted September 29, 2009 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/175880-solved-capitalize-every-other-letter/#findComment-926794 Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 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; Quote Link to comment https://forums.phpfreaks.com/topic/175880-solved-capitalize-every-other-letter/#findComment-926813 Share on other sites More sharing options...
redarrow Posted September 29, 2009 Share Posted September 29, 2009 russel cheers for the explanation, but i am to thick, to understand it but cheers...... Quote Link to comment https://forums.phpfreaks.com/topic/175880-solved-capitalize-every-other-letter/#findComment-926816 Share on other sites More sharing options...
Philip Posted September 29, 2009 Share Posted September 29, 2009 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']. Quote Link to comment https://forums.phpfreaks.com/topic/175880-solved-capitalize-every-other-letter/#findComment-926843 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.