是否可以将字符串中的字符转换为特定的数字,如
a = 1, // uppercase too
b = 2,
c = 3,
d = 4,
e = 5, // and so on til letter 'z'
space = 0 // I'm not sure about if space really is equals to 0
这是我的想法。
$string_1 = "abed"; // only string
$string_2 = "abed 5"; // with int
$result_1 = convert_to_int($string_1); // output is 1254
$result_2 = convert_to_int($string_2); // output is 125405
最佳答案
创建一个数组,并在第一个元素中插入一个空格。然后使用range()
生成一个a
到z
的数组。使用 strtolower()
强制输入小写(因为我们生成的 range()
中的字符也是小写的。
然后使用接受数组作为值的 str_replace()
进行替换。键是值将被替换的值。
function convert_to_int($string) {;
$characters = array_merge([' '], range('a', 'z'));
return str_replace(array_values($characters), array_keys($characters), $string);
}
关于php - 将非数字字符转换为特定数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55406430/