且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在PHP的正则表达式中允许撇号

更新时间:2023-02-21 11:46:49

在检查字符串之前,请勿使用htmlentities,您可以在之后使用它.

You should not use htmlentities before checking your strings, you may use it after.

如果将$name = htmlentities($name, ENT_QUOTES, "UTF-8");应用于some'name,它将变为some'name.

If you apply $name = htmlentities($name, ENT_QUOTES, "UTF-8"); to some'name, it will turn into some'name.

此外,要缩短图案,请使用

Also, to shorten the pattern, use

preg_match("/^[A-Z'-]{2,50}$/i", $name)

/i不区分大小写的修饰符将使整个模式不区分大小写,而[A-Z]将匹配所有大小写的ASCII字母.

The /i case insensitive modifier will make the whole pattern case insensitive, and [A-Z] will match all upper- and lowercase ASCII letters.