[{"data":1,"prerenderedAt":539},["ShallowReactive",2],{"content-query-3HWcXRyaSE":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"category":5,"createdAt":10,"updatedAt":10,"body":11,"_type":533,"_id":534,"_source":535,"_file":536,"_stem":537,"_extension":538},"/article/coding/how-to-use-preg_replace_callback-in-php","coding",false,"","php preg_replace_callback 客製替換文字用法","之前有個需求要將輸入的文字轉成客製想要的文字後回傳，此例也很難用一般的 preg_replace 做到，需求:","2024-06-30",{"type":12,"children":13,"toc":530},"root",[14,21,41,46,52,519,524],{"type":15,"tag":16,"props":17,"children":18},"element","p",{},[19],{"type":20,"value":9},"text",{"type":15,"tag":22,"props":23,"children":24},"ul",{},[25,31,36],{"type":15,"tag":26,"props":27,"children":28},"li",{},[29],{"type":20,"value":30},"字串前兩個字母為英文大寫",{"type":15,"tag":26,"props":32,"children":33},{},[34],{"type":20,"value":35},"第三位開始後面可能有長度小於 4 但想要用 0 補滿 4 位數字",{"type":15,"tag":26,"props":37,"children":38},{},[39],{"type":20,"value":40},"最後一位可能有字母也可能沒有，有的話想要轉大寫",{"type":15,"tag":16,"props":42,"children":43},{},[44],{"type":20,"value":45},"即當輸入為 AA1D、AA01d、AA001D、...等輸出結果為 AA0001D。",{"type":15,"tag":47,"props":48,"children":50},"h2",{"id":49},"範例",[51],{"type":20,"value":49},{"type":15,"tag":53,"props":54,"children":58},"pre",{"className":55,"code":56,"language":57,"meta":7,"style":7},"language-php shiki shiki-themes material-theme-palenight","$data = \"AA01\";\n\n$result = preg_replace_callback('/^([A-Z]{2})(\\d*)([A-Za-z]?)$/', function ($matches) {\n    $prefix = $matches[1];\n    $numberPart = str_pad($matches[2], 4, \"0\", STR_PAD_LEFT);\n    $lastChar = strtoupper($matches[3]);\n    return $prefix . $numberPart . $lastChar;\n}, $data);\n\necho $result;\n","php",[59],{"type":15,"tag":60,"props":61,"children":62},"code",{"__ignoreMap":7},[63,107,117,253,295,375,419,467,489,497],{"type":15,"tag":64,"props":65,"children":68},"span",{"class":66,"line":67},"line",1,[69,75,81,86,91,97,102],{"type":15,"tag":64,"props":70,"children":72},{"style":71},"--shiki-default:#89DDFF",[73],{"type":20,"value":74},"$",{"type":15,"tag":64,"props":76,"children":78},{"style":77},"--shiki-default:#BABED8",[79],{"type":20,"value":80},"data ",{"type":15,"tag":64,"props":82,"children":83},{"style":71},[84],{"type":20,"value":85},"=",{"type":15,"tag":64,"props":87,"children":88},{"style":71},[89],{"type":20,"value":90}," \"",{"type":15,"tag":64,"props":92,"children":94},{"style":93},"--shiki-default:#C3E88D",[95],{"type":20,"value":96},"AA01",{"type":15,"tag":64,"props":98,"children":99},{"style":71},[100],{"type":20,"value":101},"\"",{"type":15,"tag":64,"props":103,"children":104},{"style":71},[105],{"type":20,"value":106},";\n",{"type":15,"tag":64,"props":108,"children":110},{"class":66,"line":109},2,[111],{"type":15,"tag":64,"props":112,"children":114},{"emptyLinePlaceholder":113},true,[115],{"type":20,"value":116},"\n",{"type":15,"tag":64,"props":118,"children":120},{"class":66,"line":119},3,[121,125,130,134,140,145,150,154,159,164,169,174,179,184,189,194,198,202,207,212,217,222,227,233,238,243,248],{"type":15,"tag":64,"props":122,"children":123},{"style":71},[124],{"type":20,"value":74},{"type":15,"tag":64,"props":126,"children":127},{"style":77},[128],{"type":20,"value":129},"result ",{"type":15,"tag":64,"props":131,"children":132},{"style":71},[133],{"type":20,"value":85},{"type":15,"tag":64,"props":135,"children":137},{"style":136},"--shiki-default:#82AAFF",[138],{"type":20,"value":139}," preg_replace_callback",{"type":15,"tag":64,"props":141,"children":142},{"style":71},[143],{"type":20,"value":144},"(",{"type":15,"tag":64,"props":146,"children":147},{"style":71},[148],{"type":20,"value":149},"'/^",{"type":15,"tag":64,"props":151,"children":152},{"style":93},[153],{"type":20,"value":144},{"type":15,"tag":64,"props":155,"children":156},{"style":71},[157],{"type":20,"value":158},"[",{"type":15,"tag":64,"props":160,"children":161},{"style":93},[162],{"type":20,"value":163},"A-Z",{"type":15,"tag":64,"props":165,"children":166},{"style":71},[167],{"type":20,"value":168},"]{",{"type":15,"tag":64,"props":170,"children":171},{"style":93},[172],{"type":20,"value":173},"2",{"type":15,"tag":64,"props":175,"children":176},{"style":71},[177],{"type":20,"value":178},"}",{"type":15,"tag":64,"props":180,"children":181},{"style":93},[182],{"type":20,"value":183},")(",{"type":15,"tag":64,"props":185,"children":186},{"style":77},[187],{"type":20,"value":188},"\\d",{"type":15,"tag":64,"props":190,"children":191},{"style":71},[192],{"type":20,"value":193},"*",{"type":15,"tag":64,"props":195,"children":196},{"style":93},[197],{"type":20,"value":183},{"type":15,"tag":64,"props":199,"children":200},{"style":71},[201],{"type":20,"value":158},{"type":15,"tag":64,"props":203,"children":204},{"style":93},[205],{"type":20,"value":206},"A-Za-z",{"type":15,"tag":64,"props":208,"children":209},{"style":71},[210],{"type":20,"value":211},"]",{"type":15,"tag":64,"props":213,"children":214},{"style":93},[215],{"type":20,"value":216},"?)",{"type":15,"tag":64,"props":218,"children":219},{"style":71},[220],{"type":20,"value":221},"$/'",{"type":15,"tag":64,"props":223,"children":224},{"style":71},[225],{"type":20,"value":226},",",{"type":15,"tag":64,"props":228,"children":230},{"style":229},"--shiki-default:#C792EA",[231],{"type":20,"value":232}," function",{"type":15,"tag":64,"props":234,"children":235},{"style":71},[236],{"type":20,"value":237}," ($",{"type":15,"tag":64,"props":239,"children":240},{"style":77},[241],{"type":20,"value":242},"matches",{"type":15,"tag":64,"props":244,"children":245},{"style":71},[246],{"type":20,"value":247},")",{"type":15,"tag":64,"props":249,"children":250},{"style":71},[251],{"type":20,"value":252}," {\n",{"type":15,"tag":64,"props":254,"children":256},{"class":66,"line":255},4,[257,262,267,271,276,280,284,290],{"type":15,"tag":64,"props":258,"children":259},{"style":71},[260],{"type":20,"value":261},"    $",{"type":15,"tag":64,"props":263,"children":264},{"style":77},[265],{"type":20,"value":266},"prefix ",{"type":15,"tag":64,"props":268,"children":269},{"style":71},[270],{"type":20,"value":85},{"type":15,"tag":64,"props":272,"children":273},{"style":71},[274],{"type":20,"value":275}," $",{"type":15,"tag":64,"props":277,"children":278},{"style":77},[279],{"type":20,"value":242},{"type":15,"tag":64,"props":281,"children":282},{"style":71},[283],{"type":20,"value":158},{"type":15,"tag":64,"props":285,"children":287},{"style":286},"--shiki-default:#F78C6C",[288],{"type":20,"value":289},"1",{"type":15,"tag":64,"props":291,"children":292},{"style":71},[293],{"type":20,"value":294},"];\n",{"type":15,"tag":64,"props":296,"children":298},{"class":66,"line":297},5,[299,303,308,312,317,322,326,330,334,339,344,348,352,357,361,365,370],{"type":15,"tag":64,"props":300,"children":301},{"style":71},[302],{"type":20,"value":261},{"type":15,"tag":64,"props":304,"children":305},{"style":77},[306],{"type":20,"value":307},"numberPart ",{"type":15,"tag":64,"props":309,"children":310},{"style":71},[311],{"type":20,"value":85},{"type":15,"tag":64,"props":313,"children":314},{"style":136},[315],{"type":20,"value":316}," str_pad",{"type":15,"tag":64,"props":318,"children":319},{"style":71},[320],{"type":20,"value":321},"($",{"type":15,"tag":64,"props":323,"children":324},{"style":77},[325],{"type":20,"value":242},{"type":15,"tag":64,"props":327,"children":328},{"style":71},[329],{"type":20,"value":158},{"type":15,"tag":64,"props":331,"children":332},{"style":286},[333],{"type":20,"value":173},{"type":15,"tag":64,"props":335,"children":336},{"style":71},[337],{"type":20,"value":338},"],",{"type":15,"tag":64,"props":340,"children":341},{"style":286},[342],{"type":20,"value":343}," 4",{"type":15,"tag":64,"props":345,"children":346},{"style":71},[347],{"type":20,"value":226},{"type":15,"tag":64,"props":349,"children":350},{"style":71},[351],{"type":20,"value":90},{"type":15,"tag":64,"props":353,"children":354},{"style":93},[355],{"type":20,"value":356},"0",{"type":15,"tag":64,"props":358,"children":359},{"style":71},[360],{"type":20,"value":101},{"type":15,"tag":64,"props":362,"children":363},{"style":71},[364],{"type":20,"value":226},{"type":15,"tag":64,"props":366,"children":367},{"style":77},[368],{"type":20,"value":369}," STR_PAD_LEFT",{"type":15,"tag":64,"props":371,"children":372},{"style":71},[373],{"type":20,"value":374},");\n",{"type":15,"tag":64,"props":376,"children":378},{"class":66,"line":377},6,[379,383,388,392,397,401,405,409,414],{"type":15,"tag":64,"props":380,"children":381},{"style":71},[382],{"type":20,"value":261},{"type":15,"tag":64,"props":384,"children":385},{"style":77},[386],{"type":20,"value":387},"lastChar ",{"type":15,"tag":64,"props":389,"children":390},{"style":71},[391],{"type":20,"value":85},{"type":15,"tag":64,"props":393,"children":394},{"style":136},[395],{"type":20,"value":396}," strtoupper",{"type":15,"tag":64,"props":398,"children":399},{"style":71},[400],{"type":20,"value":321},{"type":15,"tag":64,"props":402,"children":403},{"style":77},[404],{"type":20,"value":242},{"type":15,"tag":64,"props":406,"children":407},{"style":71},[408],{"type":20,"value":158},{"type":15,"tag":64,"props":410,"children":411},{"style":286},[412],{"type":20,"value":413},"3",{"type":15,"tag":64,"props":415,"children":416},{"style":71},[417],{"type":20,"value":418},"]);\n",{"type":15,"tag":64,"props":420,"children":422},{"class":66,"line":421},7,[423,429,433,437,442,446,450,454,458,463],{"type":15,"tag":64,"props":424,"children":426},{"style":425},"--shiki-default:#89DDFF;--shiki-default-font-style:italic",[427],{"type":20,"value":428},"    return",{"type":15,"tag":64,"props":430,"children":431},{"style":71},[432],{"type":20,"value":275},{"type":15,"tag":64,"props":434,"children":435},{"style":77},[436],{"type":20,"value":266},{"type":15,"tag":64,"props":438,"children":439},{"style":71},[440],{"type":20,"value":441},".",{"type":15,"tag":64,"props":443,"children":444},{"style":71},[445],{"type":20,"value":275},{"type":15,"tag":64,"props":447,"children":448},{"style":77},[449],{"type":20,"value":307},{"type":15,"tag":64,"props":451,"children":452},{"style":71},[453],{"type":20,"value":441},{"type":15,"tag":64,"props":455,"children":456},{"style":71},[457],{"type":20,"value":275},{"type":15,"tag":64,"props":459,"children":460},{"style":77},[461],{"type":20,"value":462},"lastChar",{"type":15,"tag":64,"props":464,"children":465},{"style":71},[466],{"type":20,"value":106},{"type":15,"tag":64,"props":468,"children":470},{"class":66,"line":469},8,[471,476,480,485],{"type":15,"tag":64,"props":472,"children":473},{"style":71},[474],{"type":20,"value":475},"},",{"type":15,"tag":64,"props":477,"children":478},{"style":71},[479],{"type":20,"value":275},{"type":15,"tag":64,"props":481,"children":482},{"style":77},[483],{"type":20,"value":484},"data",{"type":15,"tag":64,"props":486,"children":487},{"style":71},[488],{"type":20,"value":374},{"type":15,"tag":64,"props":490,"children":492},{"class":66,"line":491},9,[493],{"type":15,"tag":64,"props":494,"children":495},{"emptyLinePlaceholder":113},[496],{"type":20,"value":116},{"type":15,"tag":64,"props":498,"children":500},{"class":66,"line":499},10,[501,506,510,515],{"type":15,"tag":64,"props":502,"children":503},{"style":136},[504],{"type":20,"value":505},"echo",{"type":15,"tag":64,"props":507,"children":508},{"style":71},[509],{"type":20,"value":275},{"type":15,"tag":64,"props":511,"children":512},{"style":77},[513],{"type":20,"value":514},"result",{"type":15,"tag":64,"props":516,"children":517},{"style":71},[518],{"type":20,"value":106},{"type":15,"tag":16,"props":520,"children":521},{},[522],{"type":20,"value":523},"輸出結果為: AA0001",{"type":15,"tag":525,"props":526,"children":527},"style",{},[528],{"type":20,"value":529},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":7,"searchDepth":109,"depth":109,"links":531},[532],{"id":49,"depth":109,"text":49},"markdown","content:article:coding:how-to-use-preg_replace_callback-in-php.md","content","article/coding/how-to-use-preg_replace_callback-in-php.md","article/coding/how-to-use-preg_replace_callback-in-php","md",1766375576046]