php 如何在php中将某些字符转换为数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3580883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert some character into numeric in php?
提问by klox
I need help to change a character in php. I got some code from the web:
我需要帮助来更改 php 中的字符。我从网上得到了一些代码:
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
回答by Peter Ajtai
Use ord()to return the ascii value. Subtract 96 to return a number where a=1, b=2....
使用ord()返回 ascii 值。减去 96 返回一个数字,其中 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.
大写和小写字母有不同的 ASCII 值,因此如果您想对它们进行相同的处理,可以使用strtolower()将大写转换为小写。
To handle the NULL
case, simply use if($dest)
. This will be true if $dest
is something other than NULL
or 0
.
要处理这种NULL
情况,只需使用if($dest)
. 如果$dest
是NULL
或以外的其他内容,则为真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';
.
PHP 是一种松散类型的语言,因此不需要声明类型。所以char dest='a';
是不正确的。变量$
在 PHP 中有前缀并且没有类型声明,所以它应该是$dest = 'a';
.
<?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.
PS: 您可以在此处查看 ASCII 值。
回答by fcingolani
This may help you: http://www.php.net/manual/en/function.ord.php
这可能对您有所帮助:http: //www.php.net/manual/en/function.ord.php
回答by Jasper
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).
它确实像示例中一样工作,除了您应该使用 php 语法(作为旁注:您发现的代码最有可能是的语言,它没有做同样的事情)。
So:
所以:
$in = "123";
$out = (int)$in;
Afterwards the following will be true:
之后,以下内容为真:
$out === 123
回答by Your Common Sense
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.
讲述整个故事而不是一点点将帮助您避免错误并节省大量时间,无论是您的还是回答问题的人。
回答by vfn
So, if you need the ASCII code you will need to do:
因此,如果您需要 ASCII 代码,则需要执行以下操作:
$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/
有关 ASCII 代码的更多信息:http: //www.asciitable.com/
And for the function ord: http://www.php.net/manual/en/function.ord.php
而对于函数 ord:http: //www.php.net/manual/en/function.ord.php