Jump to content

Is there an easier way to do this, If so how?


gergy008

Recommended Posts

	$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
Share on other sites

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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  :D!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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