Solved

Increment/Decrement Letters

Posted on 2014-04-15
9
520 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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.

706 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

18 Experts available now in Live!

Get 1:1 Help Now