Results 1 to 2 of 2

Thread: Incrementing each character in a string

  1. #1
    Dub James Guest

    Incrementing each character in a string

    My PHP isn't great so bare with me..

    I'm trying to loop through a string and increment each character depending on an int stored in a variable.

    PHP Code:
    $str "Random string";
    $chars str_split($str1);

    foreach(
    $chars as $char)
    {
        
    $char++;
        echo 
    $char;

    However, instead of printing the char to screen I'd like to replace the original character with the incremented one. Is there a simple way of doing this?

    Thanks in advance.

  2. #2
    Join Date
    May 2010
    Location
    College Station, TX
    Posts
    216
    Ceaser cipher?

    PHP Code:
    $str "Random string";
    $chars str_split($str);
    $char_shift 1;

    for(
    $i=0$i<count($chars); $i++) {
      
    $chars[$i] = $chars[$i] + $char_shift;
    }

    $new_string implode($chars);
    echo 
    $new_string
    All web designers hate the internet. If I spend all day making/updating/looking at websites, why the hell would I want to deal with it outside of work?

Similar Threads

  1. encrypt and decrypt query string
    By vannova in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 05 Jun 2007, 07:59 AM
  2. eval() if the string isn't 100% PHP code?
    By compuXP in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 18 Jul 2005, 05:37 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
  •