preg_match(): Compilation failed: UTF-8 error: byte 2 top bits not 0x80 at offset 2

Working on a laravel project I recieved When running a route with this code I didn’t write:

if(preg_match('/([ΓΑΒΓΔΕΖΗΘΙΚ])|ΣΤ|fu|ΚΑ|ΚΒ\w+/u', $char)){
   return true;
}
return false;

For exapmple whe you give the Γ character you will get the error

preg_match(): Compilation failed: UTF-8 error: byte 2 top bits not 0x80 at offset 2

on php 8.1.9 and working on laravel. the same code works fine from tinker even when passing non english characters. What is the problem

enter image description here

>Solution :

The u flag in your expression means UTF-8. You are using Windows 1253, which is not UTF-8.

The best fix is to switch to UTF-8. Click on "Windows 1253", select "Save with encoding" and pick "UTF-8". Laravel and any contemporary web application are designed around UTF-8.

Leave a Reply