Jump to content

Long question about inputting strings


axelstudios

Recommended Posts

Hello - this is my first post so I hope I'm following proper procedure.

At any rate, I've had some basic experience with PHP; enough to write a few basic scripts, some involving math.  Currently, I'm trying to write a script where a user can input a string of text, and then the script will use a cipher, and return the encrypted message.  So, what I would like to find out is, how can I have the php separate every character of the message, and turn it into something else?


ie a basic example:  The message is Hello World
The user selects the Shift Cipher with one displacement.  PHP understands, and returns:
Ifjjp Xpsme

So in this example, I just want php to change each letter, and also recognize spaces....  Is this feasable?
Link to comment
https://forums.phpfreaks.com/topic/28059-long-question-about-inputting-strings/
Share on other sites

You can also treat a string as an array and index through it.

Here's one way of doing it:
[code]<?php
$str = "Hello World";
$nstr = '';
for ($i=0;$i<strlen($str);$i++)
if ($str[$i] != ' ')$nstr .= chr(ord($str[$i]) + 1);
else $nstr .= $str[$i];
echo $str . '<br>';
echo $nstr;
?>[/code]

BTW, the Shift Cipher with one displacement of "Hello World" is not "Ifjjp Xpsme". It is "Ifmmp Xpsme".

Ken

Archived

This topic is now archived and is closed to further replies.

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