If possible, using only standard PHP functions like substr(), strrpos(), strpos(), etc.
Search for a regexp (plz correct me if I'm wrong, and how to write it in PHP):
r'x[^x]*x[^x]*$'.replace('x',your_char)