PHP preg_match_all不返回值

布兰登·斯蒂芬·海登

我有一个正则表达式,用于检查长度在4到25个字符之间的用户名,后跟任意可选的空格键和/或单个逗号。您可能已经猜到了,通过键入诸如“ Username1,Username2,Username3”之类的东西,这将用于向几个人发送个人消息。

$rule = "%^\w{4,25}( +)?,?( +)?$%";
preg_match_all($rule, $sendto, $out);
foreach($out[1] as $match) {
    echo $match;
}

尽管当我使用preg_match_all()并尝试对所有值进行排序时,正则表达式似乎正在执行其工作,但它不会向浏览器回显任何内容。我怀疑我对preg_match_all有误解,因为我的正则表达式似乎可以正常工作。

佩德罗·洛比托

您的正则表达式缺少([\w]{4,25})用户名上的捕获组,请尝试以下操作:

<?
$users = "Username1, Username2      , Username3       ";
preg_match_all('/([\w]{4,25})(?:\s+)?,?/', $users, $result, PREG_PATTERN_ORDER);
foreach($result[1] as $user) {
    echo $user;
}
/*
Username1
Username2
Username3
*/

现场演示


正则表达式说明:

([\w]{4,25})(?:\s+)?,?

Match the regex below and capture its match into backreference number 1 «([\w]{4,25})»
   Match a single character that is a “word character” (Unicode; any letter or ideograph, any number, underscore) «[\w]{4,25}»
      Between 4 and 25 times, as many times as possible, giving back as needed (greedy) «{4,25}»
Match the regular expression below «(?:\s+)?»
   Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match a single character that is a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line) «\s+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “,” literally «,?»
   Between zero and one times, as many times as possible, giving back as needed (greedy) «?»

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

PHP preg_match_all不返回值

来自分类Dev

PHP preg_match_all返回空

来自分类Dev

php从preg_match_all返回数组

来自分类Dev

PHP preg_match_all返回空

来自分类Dev

PHP的preg_match_all简单的正则表达式返回空值

来自分类Dev

PHP preg_match_all与转义引号不匹配

来自分类Dev

PHP preg_match_all未返回所需的输出

来自分类Dev

PHP的preg_match_all返回数组数组

来自分类Dev

PHP多行preg_match_all

来自分类Dev

多个通配符preg_match_all php

来自分类Dev

PHP的preg_match_all等效

来自分类Dev

preg_match_all php的奇怪行为

来自分类Dev

PHP ::清洁preg_match_all结果

来自分类Dev

PHP preg_match_all()匹配过多

来自分类Dev

PHP preg_match_all麻烦

来自分类Dev

PHP preg_match_all失败

来自分类Dev

PHP Sort preg_match_all数组

来自分类Dev

php preg_match_all说明

来自分类Dev

PHP的preg_match_all避免引号

来自分类Dev

PHP preg_match_all 换行

来自分类Dev

如何反转 preg_match_all php

来自分类Dev

preg_match_all在PHP中使用utf-8返回正确的偏移量

来自分类Dev

preg_match_all在PHP中使用utf-8返回正确的偏移量

来自分类Dev

如何在 PHP 中从带有 preg_match_all 的字符串返回 JavaScript?

来自分类Dev

PHP的preg_match_all和preg_replace

来自分类Dev

preg_match_all不返回期望值

来自分类Dev

preg_match_all不返回期望值

来自分类Dev

PHP preg_match_all的选项值中没有禁用的选项

来自分类Dev

PHP preg_match_all搜索和替换