What’s the fastest way, in PHP, to determine if some given UTF-8 text is purely ASCII or not?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A possibly faster function would be to use a negative character class (since the regex can just stop when it hits the first character, and there’s no need to internally capture anything):
Without regex (based on my comment) {
But I’d have to ask, why are you so concerned about faster? Use the more readable and easier to understand version, and only worry about optimizing it when you know it’s a problem…
Edit:
Another option is
mb_check_encoding: