Jump to content

Reversing the effect of this code


gergy008

Recommended Posts

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

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

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.