gergy008 Posted August 23, 2010 Share Posted August 23, 2010 $text=$_POST['Txt']; $strrep1=str_replace('z', 'a', $text); $strrep2=str_replace('y', 'z', $strrep1); This code is a little bit time consuming to do I'm trying to make all the letters one higher, For example "Foo bar" becomes "Gpp Cbs" The reason I'm trying to do this is a long story, But anyway is there an easier way to do it reather than the way I was doing it? And would it work? Link to comment https://forums.phpfreaks.com/topic/211548-is-there-an-easier-way-to-do-this-if-so-how/ Share on other sites More sharing options...
Alex Posted August 23, 2010 Share Posted August 23, 2010 Here's one way to do it: for($i = 0, $n = strlen($text);$i < $n;++$i) { $val = ord($text[$i]); if($val > 64 && $val < 122) { $text[$i] = chr($val + 1); } } Link to comment https://forums.phpfreaks.com/topic/211548-is-there-an-easier-way-to-do-this-if-so-how/#findComment-1102864 Share on other sites More sharing options...
Psycho Posted August 23, 2010 Share Posted August 23, 2010 Did you check the manual for str_replace()? If so, I would think the answer would jump out at you. Although, it may have just not occured to you, so don't take this as an insult. Anyway $search = array('a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z'); $replace = array('b','c','d','e','f','g','h','i','j','k','l','m','n', 'o','p','q','r','s','t','u','v','w','x','y','z','a'); $string = str_replace($search, $replace, $text); Of course, that only works for lower case letters. If you need uppercase as well, you would need to do a 2nd replacement using arrays of uppercase or use regular expression. EDIT: @AlexWD: That solution doesn't work for 'z' as requested by OP. Here is a modification of that 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); } } Link to comment https://forums.phpfreaks.com/topic/211548-is-there-an-easier-way-to-do-this-if-so-how/#findComment-1102866 Share on other sites More sharing options...
samshel Posted August 23, 2010 Share Posted August 23, 2010 <?php $str = "Sameer"; //convert string in array $arr = str_split($str); $arrMod = array(); for($intCount=0;$intCount<count($arr);$intCount++){ //Find Ascii Value $intAscii = ord($arr[$intCount]); //If a or A, make it z or Z if($intAscii == 97) $strMod = "z"; else if($intAscii == 65) $strMod = "Z"; //else find charachter with ascii value 1 less than current else $strMod = chr($intAscii-1); $arrMod[$intCount] = $strMod; } $strMod = implode($arrMod); echo $strMod; ?> just another way Link to comment https://forums.phpfreaks.com/topic/211548-is-there-an-easier-way-to-do-this-if-so-how/#findComment-1102868 Share on other sites More sharing options...
Alex Posted August 23, 2010 Share Posted August 23, 2010 I realize that my first solution wasn't the best because it wouldn't turn Z into A. Here's a better solution: for($i = 0, $n = strlen($text);$i < $n;++$i) { $val = ord($text[$i]); if($val > 64 && $val < 91) { $text[$i] = chr((($val - 64) % 26) + 65); } else if($val > 96 && $val < 123) { $text[$i] = chr((($val - 96) % 26) + 97); } } You could probably even shorten that, but in terms of readability that's more clear. Link to comment https://forums.phpfreaks.com/topic/211548-is-there-an-easier-way-to-do-this-if-so-how/#findComment-1102871 Share on other sites More sharing options...
Psycho Posted August 23, 2010 Share Posted August 23, 2010 EDIT #2: After some testng I found that my original solution does not work, but the modification I posted to AlexWD's code does. Link to comment https://forums.phpfreaks.com/topic/211548-is-there-an-easier-way-to-do-this-if-so-how/#findComment-1102874 Share on other sites More sharing options...
gergy008 Posted August 23, 2010 Author Share Posted August 23, 2010 I used this: 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); } } And it works! Thanks everyone for helping! I will mark this solved, But is there a way to reverse the process of what that code does? So it changes b to a? Link to comment https://forums.phpfreaks.com/topic/211548-is-there-an-easier-way-to-do-this-if-so-how/#findComment-1102899 Share on other sites More sharing options...
Psycho Posted August 24, 2010 Share Posted August 24, 2010 I rewrote it as a funtion that takes two parameter: the text to be modified and the offset. The offset represents how many letters forward or backward that the text should be shifted. So you can shift it 1, 2, 3 or -1, -2, -3 or whatever number of characters. function shiftLetters($text, $offset) { for($i=0, $n=strlen($text); $i<$n; ++$i) { $val = ord($text[$i]); if(($val>=65 && $val<=90) || ($val>=97 && $val<=122)) { $base = ($offset>0) ? (($val<=90)?65:97) : (($val<=90)?90:122); $text[$i] = chr($base + ($val-$base+$offset)%26); } } return $text; } echo shiftLetters('abcxyz', 2); //Ouput: cdezab echo shiftLetters('abcxyz', -2); //Ouput: yzavwx Link to comment https://forums.phpfreaks.com/topic/211548-is-there-an-easier-way-to-do-this-if-so-how/#findComment-1102970 Share on other sites More sharing options...
gergy008 Posted August 24, 2010 Author Share Posted August 24, 2010 I rewrote it as a funtion that takes two parameter: the text to be modified and the offset. The offset represents how many letters forward or backward that the text should be shifted. So you can shift it 1, 2, 3 or -1, -2, -3 or whatever number of characters. function shiftLetters($text, $offset) { for($i=0, $n=strlen($text); $i<$n; ++$i) { $val = ord($text[$i]); if(($val>=65 && $val<=90) || ($val>=97 && $val<=122)) { $base = ($offset>0) ? (($val<=90)?65:97) : (($val<=90)?90:122); $text[$i] = chr($base + ($val-$base+$offset)%26); } } return $text; } echo shiftLetters('abcxyz', 2); //Ouput: cdezab echo shiftLetters('abcxyz', -2); //Ouput: yzavwx Thanks, My problem is completly solved !! Link to comment https://forums.phpfreaks.com/topic/211548-is-there-an-easier-way-to-do-this-if-so-how/#findComment-1103386 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.