Solved

Increment/Decrement Letters

Posted on 2014-04-15
9
571 Views
Last Modified: 2014-04-30
How do you have a variable "E" and change it to either "D" or "F"?
0
Comment
Question by:breeze351
9 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40002237
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40002862
If you mean "a variable with a value of character 'E'" then the ord() and chr() are your friends.  If you mean a variable with the name "E" you have a different situation.  PHP variable names are always started with the dollar sign.  A script that refers to an unquoted E in the code refers to a constant (or potentially to an undefined constant).  If you want to post an example showing how you want to use this code in a script we could disambiguate and show you a test case.  But I expect that Dan's answer is right for what you want. :-)
0
 
LVL 33

Expert Comment

by:Slick812
ID: 40004177
greetings breeze351, , I do not see enough information in your question to give you a suggestion for your problem. As has already been said here, you might could use the PHP ord( ) to get a SINGLE text character as it's "Binary" storage numeric value. But ord( ) can only operate on Just One text character of a "string", AND the text characters have a limited range of numeric values, usually 32 to 126, , , as a value of 31 OR 127, will NOT be a valid text character, for beginners this can be difficult to understand (the "number" value of a character). If you could tell us why you ask this question, or the code problem you need to solve, then our answers might be better for you.
Ask questions if you need more information.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40004184
0
 

Author Comment

by:breeze351
ID: 40007442
What's wrong with this code?  From everything I've read, it should work.

$West_Button_West = chr(ord($West_Button_West) + 1);

It should return the next letter.

Glenn
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40007508
There may or may not be anything wrong about the code.  But in order to know we need to see the value of $West_Button_West, right?  You can print that out with

var_dump($West_Button_West);

please do that and post the result here, thanks.
0
 
LVL 33

Expert Comment

by:Slick812
ID: 40008066
you ask about this code line-
$West_Button_West = chr(ord($West_Button_West) + 1);

and as the code is written, it can work, but ONLY if the string in the $West_Button_West variable is a Single text character, AND likely for your purposes (using only a through z), if it is NOT a 'z' or a 'Z' .

You might consider a function to TEST the input string and see if it is going to be a useable string length.
My version of possible code -

function nextLetter($letter) {
// Get ONLY the first text character if strlen is more than 1
if (strlen($letter) > 1) {
  $letter = $letter[0];
  }
// rotate to first letter if end letter z
if ($letter == 'z') return 'a';
if ($letter == 'Z') return 'A';
$letter = ord($letter)+1;
// rotate to first character if end $letter number too big
if ($letter > 126) return ' ';// space character Number 32
return chr($letter);
}


$str1 = 'G';
$str1 = nextLetter($str1);

$str2 = 'bl';
$str2 = nextLetter($str2);

$str3 = 'z';
$str3 = nextLetter($str3);

echo 'nextLetter Results are '.$str1.', '.$str2.', '.$str3;

Open in new window

This works on my server and echos out -
     nextLetter Results are H, c, a
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40008549
Glenn: I think the underlying issue in your question goes to whether a character is exactly one byte.  That is the way PHP was written (back in the 1990's) but it's not the way the world works today.  There are many more alphabetic characters than can be represented by 8-bit bytes and the common, but obsolete encoding assumption of 1 byte == a character is no longer valid.  In particular, Western European characters above code point 127 are different numeric values if you choose UTF-8 versus ISO-8859-1.  That's why we need to see the output of this function:

var_dump($West_Button_West);

Simply adding the number 1 to an integer may or may not get you the code point of the "next" letter.  It may produce an invalid UTF-8 encoding.  And if the data ever needs to be transported (for example by JSON) valid UTF-8 is required.  So it's worth a little extra effort to understand modern character encoding and build your application correctly.

It's not exactly easy to get around this issue in PHP, and it's impossible if you do not understand character encoding.  You may want to read these man pages carefully.
http://php.net/manual/en/language.types.string.php
http://php.net/manual/en/language.types.string.php#language.types.string.details
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40032838
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question