Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Subtract 12 from a string? 1

Status
Not open for further replies.

jewel464g

Technical User
Jul 18, 2001
198
US
I need to be able to subtract 12 from the computer name as if they were both numbers but they are not. Make sense?

I have a friend who's done it in VB, but that doesn't help me with the syntax to do it in PHP :)

Thanks for any help.

When faced with a decision, always ask, 'Which would be the most fun?'
 
uhm?
please give examples of computernames.

eg. do you want to DIVIDE the name?
is the name like:
0202020computer and you want to seperate number from letters?

is there an fixed length of the number?
fixed length of the name?

I will check back tomorrow..
 
Make sense?
No.
Please supply an example of the computer naming scheme.
A regular expression seems to be a candidate for a solution.
 
Here's and example:

ComputerName = "OFFICECOMPUTER123"

Using only A-Z ("A" decremented by 1 will be "Z" and "A decremented by 12 will be "O") and 0-9 ("9" decremented by 1 will be "0" and "9" decremented by 12 will be "7"), how can you decrement each letter/number of the computer name by 12 characters? The result for this ComputerName will be "CTTWQSQCADIHSF345".


 
ah.. now I understand.

I've done this in VB some years ago, ROT13.. "crypting".
There is a rot13 function in PHP, but not a variable one.

you need rotX("string"); (does noe excist, so it needs to be created).

eg. a variable rot, where you can specify that it should rotate -1.

I will look into it and post you something here later today :)
 
this is limited to DECREASE of 1..
new version might follow, if I have too much time on my hands.

Code:
<?php

if (!isset($submit))
  {
    echo "<form action=\"?\" method=\"post\">
            <input type=\"text\" name=\"computername\" />
            <input type=\"submit\" name=\"submit\" value=\"rotX\" />
            </form>";
  }
else
  {
    $az = generateChr($computername);
  }
 
function generateChr($letter)
  {
    if (ord($letter) == 97) // if = a
      {
        $letter = "z"; // set to z
      }
    else if (ord($letter) == 65) // if = A
      {
        $letter = "Z"; // set to Z
      }
    else if (ord($letter) == 48) // if = 0
      {
        $letter = chr(ord($letter) + 9); // set to 9
      }
    else
      {
        $letter = chr(ord($letter) - 1); // decrease 1 in dec value on this table: [URL unfurl="true"]http://www.asciitable.com[/URL]
      }
  
echo $letter;
  }

?>
 
I'm just curious how mlstewart was able to supply the example for a question asked by jewel464g. If you have two user ids would you stick with one - or is mlstewart hijacking the post?
 
mlstewart is the friend that jewel464g referred to in the original post.
 
Ok you two friends. Here's a simple solution that does exactly what you want. You can wrap it into a function - and you'll see, it is really simple:
Code:
# easy solution
$computername = "OFFICECOMPUTER123";
$from = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$to   = "OPQRSTUVWXYZABCDEFGHIJKLMN2345678901";
$encoded = strtr($computername,$from,$to);
 
Did you have any luck with the supplied solution?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top