There’s no way your version works because the str_replace is executed immediately and the $2 isn’t available at that time. The regex would either have to use a callback or be expanded to catch words (or look on php.net for the /e modifer)