Improve performance of PHP ustring

Cache patterns and the regexes they become, avoid revalidating the same
pattern multiple times, and don't bother checking if something is a string
when we just made it one.

Change-Id: I1a61dd0a36eb449c8acdc8c1be68aae793f172d3