douyan1244 2017-05-19 19:35
浏览 42
已采纳

如何用特定字母替换特定数字(在长度限制内)?

I am trying to use preg_replace to change numbers to letters, but only numbers wrapped in "" and not more than 7 characters in the substring.

Sample input string:

"3"sca"""co"1"str"0"ctor""r"3"t"0"r"1""locat"5"o"133""0"27""754a49b393c2a0"33"b97"332"cb7"3"c3c07"2""co"1"str"0"ctor""r"3"t"0"

The desired effect is for every qualifying 2 to become d and every qualifying 3 to become e.

These are examples of correct replacements:

  • "3" becomes e
  • "23" becomes de
  • "33" becomes ee
  • "32" becomes de
  • "333223" becomes eeedde

My coding attempt:

$string = preg_replace("/\"322\"+/", "edd", $string);
$string = preg_replace("/\"233\"+/", "dee", $string);
$string = preg_replace("/\"32\"+/", "ed", $string);
$string = preg_replace("/\"23\"+/", "de", $string);
$string = preg_replace("/\"33\"+/", "e", $string);
$string = preg_replace("/\"333\"+/", "e", $string);
$string = preg_replace("/\"3\"+/", "e", $string);
$string = preg_replace("/\"3\"+/", "e", $string);
$string = preg_replace("/\"3\"+/", "e", $string);
$string = preg_replace("/\"3\"+/", "e", $string);
$string = preg_replace("/\"3\"+/", "e", $string);

How can I make all qualifying replacements with one preg_replace call?

  • 写回答

3条回答 默认 最新

  • dongxiangshen7916 2017-05-19 22:27
    关注

    Use this regex to find either 2 or 3 between double-quotes from 1 to 7 times

    (?<=\")[23]{1,7}(?=\")

    "233223322" won't be replaced with "deeddeedd" because it has more than 7 characters (9)

    Demo

    Explanation

    [23]{1,7} Either 2 or 3 from 1 to 7 times

    (?<=\") Precedeed by a double-quote

    (?=\") Followed by a double-quote

    Snippet

    $text = '"3"sca"""co"1"str"0"ctor""r"3"t"0"r"1""locat"5"o"133""0"27""754a49b393c2a0"33"b97"332"cb7"3"c3c07"2""co"1"str"0"ct"233223322"or""r"3"t"0"';
    
    $regex = '/(?<=\")[23]{1,7}(?=\")/';
    
    $text = preg_replace_callback($regex, 
        function ($m) {
            switch ($m[0]) {
                case '2': return 'd';
                case '3': return 'e';
                // Add other cases here ...
            }
        },
        $text
    );
    
    echo $text;
    

    Online demo

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?