dongmi4035 2012-10-03 18:26
浏览 40
已采纳

用数组值替换字符串中的字符

I have the following array

static public $tabla = array(
            'A'=>2, 'B'=>2, 'C'=>2,
            'D'=>3, 'E'=>3, 'F'=>3,
            'G'=>4, 'H'=>4, 'I'=>4,
            'J'=>5, 'K'=>5, 'L'=>5,
            'M'=>6, 'N'=>6, 'O'=>6,
            'P'=>7, 'Q'=>7, 'R'=>7,
            'S'=>8, 'T'=>8, 'U'=>8,
            'V'=>9, 'W'=>9, 'X'=>9,
            'Y'=>0, 'Z'=>0
            );

And several strings consisting of alphanumeric characters, for example: "G20513F4561B". What is most efficient way of replacing the non-numeric characters in string by its equivalence according the given array?

I know this can be easily implemented using a loop but maybe there is some kind of regular expression that does the trick.

Thanks

  • 写回答

2条回答 默认 最新

  • douhan4093 2012-10-03 18:34
    关注

    Try strtr():

    echo strtr($str, self::$tabla);
    

    Keys will be replaced with their corresponding values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失