gpt4 book ai didi

PHP 正则表达式,字符为 'ä'

转载 作者:行者123 更新时间:2023-12-04 04:38:44 28 4
gpt4 key购买 nike

<?
$s = '';
$string1 = '<script>';
$string2 = 'färsk';
$string3 = 'öde';

$rex = "/[\^<@\/\{\}\!\*\$%\?=≤>€:\|;#]+/i";
if (preg_match($rex,$string1)) { $s = NULL; }
if ($s === NULL) { echo 'null'; } else { echo "not null"; }
?>

为什么 $string2 给 null ?
'ä' 字符有什么特别之处?其他瑞典语 å 和 ö 很好地通过了正则表达式。

感谢对我的正则表达式的任何修复。我希望它允许除该正则表达式中定义的特殊字符之外的所有内容。

编辑:

澄清:
我想要做的是让除特殊字符列表之外的任何内容通过。 $string2 和 3 都应该通过,$string 1 不应该通过(因为 < 和 >)。

最佳答案

避免不必要的逃避。见下面的代码:

$string1 = '<script>';
$string2 = 'färsk';
$string3 = 'öde';

$rex = "/^[^<>@\/{}!*$%?=≤€:|;#]+$/u";

var_dump (preg_match($rex, $string1)); // false
var_dump (preg_match($rex, $string2)); // true as no special char in character class
var_dump (preg_match($rex, $string3)); // true as no special char in character class

关于PHP 正则表达式,字符为 'ä',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19255810/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com