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

huangapple go评论58阅读模式
英文:

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

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

答案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.

huangapple
  • 本文由 发表于 2023年2月16日 18:16:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定