Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts

    any clue on how to do this? < charcode>

    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?
    flying dagger

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    hmm, well try this:

    PHP Code:
    /*
    * 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, -1PREG_SPLIT_NO_EMPTY);
    foreach(
    $chars As $Chr){
        echo 
    $CharsArray[(array_search($Chr,$CharsArray) + 10)];
    }

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

  • #3
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Possibly something like:

    Code:
    $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?
    Last edited by MattF; 02-11-2010 at 08:22 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •