Solved

Increment/Decrement Letters

Posted on 2014-04-15
9
625 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40002237
0
 
LVL 110

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 34

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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

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 110

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 34

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 110

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 110

Expert Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

689 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