gergy008 Posted August 24, 2010 Share Posted August 24, 2010 Okay, Earlier on I asked someone if they could show me a shorter way of making letters one higher, Like A becomes B and B becomes C in a string. I was wondering if using this code: for($i=0, $n=strlen($text); $i<$n; ++$i) { $val = ord($text[$i]); if(($val>=65 && $val<=90) || ($val>=97 && $val<=122)) { $base = ($val<=90) ? 65 : 97; $text[$i] = chr($base + ($val-$base+1)%26); } } I could reverse it's effect and make A become Z and B become A in a string. Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/211564-reversing-the-effect-of-this-code/ Share on other sites More sharing options...
fortnox007 Posted August 26, 2010 Share Posted August 26, 2010 I would love to know that too, I can only think of a str_replace() way Link to comment https://forums.phpfreaks.com/topic/211564-reversing-the-effect-of-this-code/#findComment-1103845 Share on other sites More sharing options...
AbraCadaver Posted August 26, 2010 Share Posted August 26, 2010 What's wrong with strtoupper() and strtolower()? Link to comment https://forums.phpfreaks.com/topic/211564-reversing-the-effect-of-this-code/#findComment-1104025 Share on other sites More sharing options...
AbraCadaver Posted August 26, 2010 Share Posted August 26, 2010 What's wrong with strtoupper() and strtolower()? Sorry brain-fart, never mind... In general this increments and the next one decrements: $new = chr(ord($letter) + 1); $new = chr(ord($letter) - 1); You need to decide what happens if you try and decrement an A or increment a Z, etc... Link to comment https://forums.phpfreaks.com/topic/211564-reversing-the-effect-of-this-code/#findComment-1104043 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.