Solved

Increment/Decrement Letters

Posted on 2014-04-15
9
547 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 108

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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40004184
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 108

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 108

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 108

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WordPress TK Title 8 25
hiding/removing php extension - best practice 4 27
Checking CSRF token within a function 36 16
hasing a url 16 0
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now