用数字字符串中的ASCII值替换char

Hi I have the following string consisting of letters and numbers "00G990010020100038" and I want to replace the letter for their ASCII value, in fact there can be only one letter inside the string, the position is what it changes.

I tried using preg_replace with no success, maybe I'm doing something wrong.

preg_replace("/[a-zA-Z]/", ord('$\1'), $mystring)

My code substitutes the letter for ASCII value of character '$' but I want 'G' (in this case) to be replaced.

Try this:

preg_replace("/[a-zA-Z]/e", "ord('\\0')", $mystring)

The /e modifier lets you execute PHP in the replacement.

You could use preg_replace_callback instead. It's more efficient than the e modifier.

preg_replace_callback(
    "/[a-zA-Z]/",
    create_function('$matches','return ord($matches[0]);'),
    $mystring
);