15

I need help to change a character in php. I got some code from the web:

char dest='a';
int conv=(int)dest;

Can I use this code to convert a character into numeric? Or do you have any ideas? I just want to show the result as a decimal number:

if null == 0
if A == 1
Luke_0
  • 779
  • 5
  • 20
klox
  • 2,089
  • 11
  • 38
  • 65
  • 6
    What number do you want? The ascii code? – vfn Aug 27 '10 at 02:17
  • No you can't use that code - it isn't php. – Jasper Aug 27 '10 at 02:22
  • i'm just need standard number like 0,1,2,3... – klox Aug 27 '10 at 02:23
  • "just want to show result as decimal number" **What number?!** A letter is not a number. You need to define how a letter would correspond to a number. Should 'A' correspond to 1? Do you want the ASCII code where 'A' corresponds to 65? – deceze Aug 27 '10 at 02:31
  • @deceze: i want if "null == 0" and if i type "A == 1". – klox Aug 27 '10 at 02:34
  • I understand less each time you edit your question. – BrunoLM Aug 27 '10 at 02:39
  • 1
    Oh come on, it's finally rather cleared up. :) a-z/A-Z (just assuming case doesn't matter) should be converted to 1-26, and `null`/`false` values to 0. – deceze Aug 27 '10 at 02:47

6 Answers6

53

Use ord() to return the ascii value. Subtract 96 to return a number where a=1, b=2....

Upper and lower case letters have different ASCII values, so if you want to handle them the same, you can use strtolower() to convert upper case to lower case.

To handle the NULL case, simply use if($dest). This will be true if $dest is something other than NULL or 0.

PHP is a loosely typed language, so there is no need to declare the types. So char dest='a'; is incorrect. Variables have $ prefix in PHP and no type declaration, so it should be $dest = 'a';.

Live Example

<?php

    function toNumber($dest)
    {
        if ($dest)
            return ord(strtolower($dest)) - 96;
        else
            return 0;
    }

      // Let's test the function...        
    echo toNumber(NULL) . " ";
    echo toNumber('a') . " ";
    echo toNumber('B') . " ";
    echo toNumber('c');

      // Output is:
      // 0 1 2 3
?>

PS: You can look at the ASCII values here.

Peter Ajtai
  • 56,972
  • 13
  • 121
  • 140
  • Doesn't handle upper case correctly, but seems to be on the right track. – deceze Aug 27 '10 at 02:33
  • @deceze - That's because upper and lower have different ASCII values. I edited the answer so that upper case is handled the same way as lower case. – Peter Ajtai Aug 27 '10 at 02:37
  • how if null? i want if null == 0 – klox Aug 27 '10 at 02:37
  • @klox - I edited the answer so that `toNumber` uses an `if ... else ...` statement to hand the `NULL` & `0` cases. You can also use `is_null()` to check for only null, but I think `if($dest)` works better in this case. – Peter Ajtai Aug 27 '10 at 02:42
  • used this answer to as part of an answer to a different question of my own found [here](http://stackoverflow.com/questions/22566638/phpexcel-how-to-get-only-1-cell-value/22566639) – CrandellWS Mar 21 '14 at 18:25
3

It does indeed work as in the sample, except that you should be using php syntax (and as a sidenote: the language that code you found most probably was, it did not do the same thing).

So:

$in = "123";
$out = (int)$in;

Afterwards the following will be true:

$out === 123
Jasper
  • 11,590
  • 6
  • 38
  • 55
  • is this not reversed? I wish if null mean zero and if I type A the result is 1 – klox Aug 27 '10 at 02:31
  • @Brendan Long: I interpreted `i'm just need standard number like 0,1,2,3` as him saying he wanted this, but as you say it's not clear. I added a result to my answer so it's clear what this does. – Jasper Aug 27 '10 at 02:36
2

This may help you: http://www.php.net/manual/en/function.ord.php

fcingolani
  • 581
  • 2
  • 5
1

So, if you need the ASCII code you will need to do:

$dest = 'a';
$conv = ord($dest);

If you want something like:

a == 1
b == 2
.
.
.

you should do:

$dest = 'a';
$conv = ord($dest)-96;

For more info on the ASCII codes: http://www.asciitable.com/

And for the function ord: http://www.php.net/manual/en/function.ord.php

vfn
  • 6,026
  • 2
  • 34
  • 45
1

It's very hard to answer because it's not a real question but just a little bit of it.
But if you ask.
It seems you need some translation table, that defines links between letters and numbers

A -> 2
B -> 3
C -> 4
S -> 1

or whatever.
You can achieve this by using an array, where keys would be these letters and values - desired numbers.

$defects_arr = array(
    'A' -> 2,
    'B' -> 3,
    'C' -> 4'
    'S' -> 1
};

Thus, you can convert these letters to numbers

$letter = 'A';
$number = $defects_arr($letter);
echo $number; // outputs 1

But it still seems is not what you want.
Do these defect types have any verbose equivalents? If so, why not to use them instead of letters?

Telling the whole story instead of little bit of it will help you to avoid mistakes and will save a ton of time, both yours and those who to answer.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

Out of this question, if you are looking for convert RT0005 to 5

$max = 'RT0005';
return base_convert($max,10,10);
// return 5
YasirPoongadan
  • 683
  • 6
  • 19