英文:
preg_match(): Compilation failed: UTF-8 error: byte 2 top bits not 0x80 at offset 2
问题
在处理 Laravel 项目时,我收到了以下代码,运行包含我没有编写的路由时出现了问题:
if(preg_match('/([ΓΑΒΓΔΕΖΗΘΙΚ])|ΣΤ|fu|ΚΑ|ΚΒ\w+/u', $char)){
return true;
}
return false;
例如,当输入字符 'Γ' 时,您会遇到以下错误:
preg_match(): Compilation failed: UTF-8 error: byte 2 top bits not 0x80 at offset 2
这在 PHP 8.1.9 中运行,并且在 Laravel 中出现问题。即使通过 tinker 传递非英语字符,相同的代码也可以正常运行。问题是什么?
英文:
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
答案1
得分: 1
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.
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论