...

View Full Version : any clue on how to do this? < charcode>



xiaodao
02-11-2010, 06:15 PM
chars sequence as "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

now i have a string " hello 2", i want to convert this string by replace each character with 10th more position which means

replace "h" with "r"
replace "e" with "o";
replace "2" with "B";

something like these. Any clue?

angst
02-11-2010, 08:06 PM
hmm, well try this:



/*
* num: 48-57
* lcase: 97-122
* ucase: 65-90
*/

$lcase = 97;
$strLcase = array();
while($lcase <= 122){
$strLcase[] = chr($lcase);
$lcase++;
}

$num = 48;
$StrNum = array();
while($num <= 57){
$StrNum[] = chr($num);
$num++;
}

$ucase = 65;
$strUcase = array();
while($ucase <= 90){
$strUcase[] = chr($ucase);
$ucase++;
}

$CharsArray = array_merge($strLcase, $StrNum, $strUcase);

$string = "HELLO!";
$chars = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);
foreach($chars As $Chr){
echo $CharsArray[(array_search($Chr,$CharsArray) + 10)];
}

output: ROVVYk


it works, though I'm sure there are much better ways to do this.

MattF
02-11-2010, 08:16 PM
Possibly something like:



$word = 'teststring';
$word = str_split($word);

$chars = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

foreach ($word as $letter)
{
$output .= substr($chars, (strpos($chars, $letter)+10), 1);
}


What do you expect to happen if anything within the last ten letters of $chars are in $word though?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum