没有模式分隔符的正则表达式不会使用 preg_match() 发出警告

发布于 2024-10-09 18:40:15 字数 531 浏览 0 评论 0原文

$first = ".";
$last = "a";
$middle= "testing-123.test";

if (preg_match("[a-zA-Z0-9]", $first)){
 echo 'Frist Returned valid.';
}
if (preg_match("[a-zA-Z0-9.-]", $middle)){
 echo 'Middle Returned valid.';
}
if (preg_match("[a-zA-Z0-9]", $last)){
 echo 'Last Returned valid.';
}

我使用 substr 将字符串分解为 3 部分,因此在本例中原始文本为“.testing-123.testa”。我在这里没有包含 substr,只是预定义了本例中的变量。我想检查一下,这意味着以下要求:

第一个字符:az,AZ,0-9无符号

中间字符:az,AZ,0-9,仅。和 - 符号

最后一个字符:与第一个字符相同。

当我运行上面的代码时,它没有任何回显。有什么想法吗?

$first = ".";
$last = "a";
$middle= "testing-123.test";

if (preg_match("[a-zA-Z0-9]", $first)){
 echo 'Frist Returned valid.';
}
if (preg_match("[a-zA-Z0-9.-]", $middle)){
 echo 'Middle Returned valid.';
}
if (preg_match("[a-zA-Z0-9]", $last)){
 echo 'Last Returned valid.';
}

I am breaking down a string into 3 parts using substr so in this example the original text was ".testing-123.testa". I didnt include the substr here but just predefined the variables in this case. I want to check so it means the following requirements:

First character: a-z, A-Z, 0-9 NO SYMBOLS

middle character: a-z, A-Z, 0-9, only . and - symbols

Last character: same as first character.

When I ran above code it echo'd nothing. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多像笑话 2024-10-16 18:40:15

您缺少分隔符,请尝试在正则表达式周围添加 /

if (preg_match("/[a-zA-Z0-9]/", $first)){
 echo 'Frist Returned valid.';
}
if (preg_match("/[a-zA-Z0-9.-]/", $middle)){
 echo 'Middle Returned valid.';
}
if (preg_match("/[a-zA-Z0-9]/", $last)){
 echo 'Last Returned valid.';
}

您可能还想使用 preg_match_all 来查找更多/所有实例。

You are missing delimitters, try adding / around your regex:

if (preg_match("/[a-zA-Z0-9]/", $first)){
 echo 'Frist Returned valid.';
}
if (preg_match("/[a-zA-Z0-9.-]/", $middle)){
 echo 'Middle Returned valid.';
}
if (preg_match("/[a-zA-Z0-9]/", $last)){
 echo 'Last Returned valid.';
}

You might also want to use preg_match_all to find more/all instances.

奈何桥上唱咆哮 2024-10-16 18:40:15
if (preg_match("/^[a-z0-9]+$/i", $first)){
 echo 'First Returned valid.';
}
if (preg_match("/^[a-z0-9.-]+$/i", $middle)){
 echo 'Middle Returned valid.';
}
if (preg_match("/^[a-z0-9]+$/i", $last)){
 echo 'Last Returned valid.';
}

应该做你需要的。

您需要分隔符(/),并且需要确保从字符串的开头到结尾测试所有字符(^$锚点)。

if (preg_match("/^[a-z0-9]+$/i", $first)){
 echo 'First Returned valid.';
}
if (preg_match("/^[a-z0-9.-]+$/i", $middle)){
 echo 'Middle Returned valid.';
}
if (preg_match("/^[a-z0-9]+$/i", $last)){
 echo 'Last Returned valid.';
}

should do what you need.

You need delimiters (/), and you need to make sure that all characters are tested from the beginning to the end of the string (^ and $ anchors).

尬尬 2024-10-16 18:40:15

无论您的编码意图如何,所有正则表达式模式都将左方括号和右方括号视为模式分隔符。发生这种情况是因为某些不同的字符可以用作模式分隔符。由于您的模式在技术上是有效的,因此不会发出警告来抱怨缺少分隔符。因此,您的字符类不会被视为字符类,而是被解释为主要包含文字字符的模式(仅 . 被视为元字符)。 展示 a-zA-Z0-9a-zA-Z0-9 的演示#- 将被视为有效。 一个基本的解决方案是将模式括在正斜杠中。

此外,您的模式不会验证整个字符串——它们仅检查是否有 1 个字符匹配。要验证完整字符串,您需要将字符串的开头与 ^ 匹配,需要一个或多个字符与 + 匹配,然后将字符串的结尾与 匹配>$。

最终,只有在没有更简单的解决方案时才应使用正则表达式。要简单地检查字符串是否完全由字母数字字符组成,最理想的调用是 ctype_alnum()

推荐解决方案:(演示

$first = ".";
$last = "a";
$middle= "testing-123.test";

echo "First segment:\t" . (ctype_alnum($first) ? 'valid' : 'invalid') . "\n";
echo "Second segment:\t" . (preg_match("/^[a-z\d.-]+$/i", $middle) ? 'valid' : 'invalid') . "\n";
echo "Third segment:\t" . (ctype_alnum($last) ? 'valid' : 'invalid');

输出:

First segment:  invalid
Second segment: valid
Third segment:  valid

Despite your coding intention, all of your regex patterns are treating the opening square brace and the closing square brace as pattern delimiters. This is happening because some non-identical characters can be used as pattern delimiters. Because your pattern is technically valid, no Warning is emitted to complain about missing delimiters. As a consequence, your character class is not treated as a characters class and is instead interpreted as a pattern containing mostly literal characters (only the . is treated as a metacharacter). Demo showing that a-zA-Z0-9 and a-zA-Z0-9#- will be deemed valid. A basic solution is to wrap your patterns in forward slashes.

Also, your patterns do not validate the entire string -- they only check if as little as 1 character is matched. To validate the full string, you need to match the start of the string with ^, require one or more characters with +, then match the end of the string with $.

Ultimately, regex should only be used when there isn't a simpler solution. To simply check if a string is completely comprised of alphanumeric characters, the most ideal call is ctype_alnum().

Recommended solution: (Demo)

$first = ".";
$last = "a";
$middle= "testing-123.test";

echo "First segment:\t" . (ctype_alnum($first) ? 'valid' : 'invalid') . "\n";
echo "Second segment:\t" . (preg_match("/^[a-z\d.-]+$/i", $middle) ? 'valid' : 'invalid') . "\n";
echo "Third segment:\t" . (ctype_alnum($last) ? 'valid' : 'invalid');

Output:

First segment:  invalid
Second segment: valid
Third segment:  valid
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文