回答編集履歴
3
修正
    
        answer	
    CHANGED
    
    | @@ -22,8 +22,8 @@ | |
| 22 22 | 
             
            echo const_replace("hello2{HOGE}{FUGA}");
         | 
| 23 23 |  | 
| 24 24 | 
             
            function const_replace($str){
         | 
| 25 | 
            -
                foreach(get_defined_constants(true)['user'] as $key => $ | 
| 25 | 
            +
                foreach(get_defined_constants(true)['user'] as $key => $val){
         | 
| 26 | 
            -
                    $str = str_replace('{' . $key . '}', $ | 
| 26 | 
            +
                    $str = str_replace('{' . $key . '}', $val, $str);
         | 
| 27 27 | 
             
                }
         | 
| 28 28 | 
             
                return $str;
         | 
| 29 29 | 
             
            }
         | 
2
修正
    
        answer	
    CHANGED
    
    | @@ -28,5 +28,5 @@ | |
| 28 28 | 
             
                return $str;
         | 
| 29 29 | 
             
            }
         | 
| 30 30 | 
             
            ```
         | 
| 31 | 
            -
            str_replace がベースなので、当 | 
| 31 | 
            +
            str_replace がベースなので、当然ですが置換文字列によっては誤作動します。
         | 
| 32 32 | 
             
            参考程度のものとお考えください。
         | 
1
追記
    
        answer	
    CHANGED
    
    | @@ -10,4 +10,23 @@ | |
| 10 10 | 
             
            echo "hello" . HOGE;
         | 
| 11 11 | 
             
            echo sprintf("hello%s", HOGE);
         | 
| 12 12 | 
             
            echo str_replace("{HOGE}", HOGE, "hello{HOGE}");
         | 
| 13 | 
            -
            ```
         | 
| 13 | 
            +
            ```
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            **追記**
         | 
| 16 | 
            +
            ごく簡単な置換用の関数を作ってみました。
         | 
| 17 | 
            +
            ```php
         | 
| 18 | 
            +
            <?php
         | 
| 19 | 
            +
            const HOGE = '123';
         | 
| 20 | 
            +
            echo const_replace("hello{HOGE}");
         | 
| 21 | 
            +
            const FUGA = '456';
         | 
| 22 | 
            +
            echo const_replace("hello2{HOGE}{FUGA}");
         | 
| 23 | 
            +
             | 
| 24 | 
            +
            function const_replace($str){
         | 
| 25 | 
            +
                foreach(get_defined_constants(true)['user'] as $key => $serch){
         | 
| 26 | 
            +
                    $str = str_replace('{' . $key . '}', $serch, $str);
         | 
| 27 | 
            +
                }
         | 
| 28 | 
            +
                return $str;
         | 
| 29 | 
            +
            }
         | 
| 30 | 
            +
            ```
         | 
| 31 | 
            +
            str_replace がベースなので、当山ですが置換文字列によっては誤作動します。
         | 
| 32 | 
            +
            参考程度のものとお考えください。
         | 
