This is really not secure, but I used eval() here, just for the sake of simplicity. You can replace it with your switch() case logic. I think the one of the ways to do what you want is with preg_replace_callback():