PHP str_replace

前端 未结 7 2084
渐次进展
渐次进展 2021-01-25 11:03

I have the string $var in which I need to replace some text. The first \"X\" needs to be replaced by \"A\", the second \"X\" needs to be replaced by B and so on, here is an exam

7条回答
  •  情深已故
    2021-01-25 11:17

    You could use preg_replace_callback():

    // as many as you think you'll need, maximum.
    // this can be programmatically generated, if need be
    $replacements = array('A', 'B', 'C', 'D', 'E', 'F', 'G'); // and so on    
    
    function get_replace_value($matches) {
        global $replacements;
        return array_shift($replacements);
    }
    
    $var = preg_replace_callback("/" + preg_quote($needle) + "/",
        "get_replace_value", $var);
    

提交回复
热议问题