axelstudios Posted November 22, 2006 Share Posted November 22, 2006 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 WorldThe user selects the Shift Cipher with one displacement. PHP understands, and returns:Ifjjp XpsmeSo 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 More sharing options...
fert Posted November 22, 2006 Share Posted November 22, 2006 explode will split a string Link to comment https://forums.phpfreaks.com/topic/28059-long-question-about-inputting-strings/#findComment-128388 Share on other sites More sharing options...
kenrbnsn Posted November 22, 2006 Share Posted November 22, 2006 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 Link to comment https://forums.phpfreaks.com/topic/28059-long-question-about-inputting-strings/#findComment-128390 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.