开发者

How to convert some character into numeric in php?

开发者 https://www.devze.com 2023-01-13 02:17 出处:网络
I need help to change a character in php. I got some code from the web: char dest=\'a\'; int conv=(int)dest;

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 numeri开发者_运维知识库c? Or do you have any ideas? I just want to show the result as a decimal number:

if null == 0
if A == 1


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.


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


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


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


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.


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

$max = 'RT0005';
return base_convert($max,10,10);
// return 5
0

精彩评论

暂无评论...
验证码 换一张
取 消