简体   繁体   English

PHP使用str_replace将数字更改为字符串而不混淆

[英]PHP changing numbers to the strings with str_replace without mixed up

I am changing numbers to the strings. 我正在将数字更改为字符串。 Like that: 像那样:

1=>A
2=>B
3=>C
4=>D
5=>E
6=>F
7=>G
8=>H
9=>I
10=>J
11=>K
12=>L

I made this function to be replace: 我已将此函数替换为:

function name($string) {
    $find=array("1","2","3","4","5","6","7","8","9","10","11","12");
    $replace=array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L");
    $string=str_replace($find,$replace,$string);  
    return $string;
}

But if i use this: name("12"); 但是如果我使用这个: name("12"); its not returning L its return AB . 其不返回L其返回AB

Actually its make sense. 实际上,这是有道理的。 Returning each letter. 返回每个字母。 How can i return L in this function? 如何在此函数中返回L What should i do? 我该怎么办? Thanks in advance. 提前致谢。

The str_replace() with array replace provided string with any match in array items. 带有数组的str_replace()将提供的字符串替换为数组项中的任何匹配项。 So use array_search() like shown in bottom instead 因此,请使用如底部所示的array_search()代替

$result = $replace[array_search($string, $find)]; 

So your code changed to 所以你的代码改为

function name($string) {
    $find = array("1","2","3","4","5","6","7","8","9","10","11","12");
    $replace = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L");  
    return $replace[array_search($string, $find)]; 
}

Check result in demo 演示中检查结果

The problem is that str_replace() just replaces them in the order that you specify as stated in the documentation ... 问题是str_replace()只是按照文档中指定的顺序替换它们。

Replacement order gotcha 更换订单陷阱

Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. 由于str_replace()从左到右替换,因此在进行多次替换时可能会替换先前插入的值。 See also the examples in this document. 另请参阅本文档中的示例。

If you instead use strtr() it will replace them longest first... 如果您改为使用strtr() ,它将首先替换最长的一个...

$string=strtr($string, array_combine($find, $replace));

You do have a PHP function for what you are trying to achieve - chr 您确实具有要实现的PHP功能-CHR

<?php 

function name($string) {
   return chr(intval($string) + 64);
}

UPDATE: 更新:

  • Thanks to @Ulrich. 感谢@Ulrich。 You can better describe the above solution using ord('A') . 您可以使用ord('A')更好地描述上述解决方案。 So, basically you add the ASCII value of A to the integer converted string parameter and subtract 1 from it, to get the ASCII value for the character we are seeking. 因此,基本上,您将A的ASCII值添加到整数转换的字符串参数中,并从中减去1,以获得我们要查找的字符的ASCII值。 chr() finally gives us that character. chr()最终给了我们该字符。

function name($string) {
   return chr(intval($string) + ord('A') - 1);
}

Create a simple mapping: 创建一个简单的映射:

$mapping = [
    '1' => 'A',
    '2' => 'B',
    '12' => 'L',
];
if (!array_key_exists($string, $mapping)) {
    throw new Exception('invalid input provided');
}
return $mapping[$string];

If I don't understood your question then , Try with range() instead of long key=>value array. 如果我不明白您的问题 ,请尝试使用range()而不是long key => value数组。 I think for your current requirement str_repalce() is not a good choice, simple range() will do the magic :) 我认为对于您当前的需求str_repalce()并不是一个不错的选择,简单的range()可以解决问题:)

<?php
  function name($num){
    $array = range('A','Z');
    $array = array_filter(array_merge(array(0), $array));
    return $array[$num];
  }

 echo name(12);

DEMO: https://3v4l.org/kl9jn 演示: https : //3v4l.org/kl9jn

instead of str_replace use chr 代替str_replace使用chr

function name($string) {
    return chr( $string + 64 );
}

note that this will word as long a $string is actually containing a number in the range 1 .. 26 请注意,只要$string实际上包含范围为1 .. 26的数字,它将用词表示


But if -for some reason- you want to stick with the str_replace approach a solution is to pad numbers with a leading and following space (or a character of your choice...), this way: 但是,如果出于某种原因,您想要坚持使用str_replace方法,一种解决方案是使用前导和后继空格(或您选择的字符...)填充数字,方法是:

function name($string) {
    $string = " " . $string . " ";
    $find=array(" 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "," 9 "," 10 "," 11 "," 12 ");
    $replace=array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L");
    $string=str_replace($find,$replace,$string);  
    return $string;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM