PHP正则表达式工作不正常
我试图根据字母数字或下划线来提取数组中的一些值。它没有成功。这就是我提供的脚本:PHP正则表达式工作不正常,php,mysql,regex,Php,Mysql,Regex,我试图根据字母数字或下划线来提取数组中的一些值。它没有成功。这就是我提供的脚本: ... [183]=> string(23) "/games/boomerang_devils" [184]=> string(26) "/games/krushuna_waterfalls" [185]=> string(21) "/games/super_drift_3d" [186]=> string(34) "/games/around_the_wor
...
[183]=>
string(23) "/games/boomerang_devils"
[184]=>
string(26) "/games/krushuna_waterfalls"
[185]=>
string(21) "/games/super_drift_3d"
[186]=>
string(34) "/games/around_the_world_in_80_days"
[187]=>
string(24) "/games/governor_of_poker"
[188]=>
string(21) "/games/shoot_on_sight"
...
守则:
if (preg_match('/\/games\/[a-zA-Z0-9_]/', $array['item'])) {
// Add to DB
}
但它没有把它捡起来。谁能看出我做错了什么
[a-zA-Z0-9_] is one character only.
您需要通过添加+
if (preg_match('/\/games\/[a-zA-Z0-9_]+/', $array))
{
// Add to DB
}
/\/games\/[a-zA-Z0-9_]+/
对$array
变量无效。它需要一个字符串。因此,要么在列表上循环,然后测试每个字符串。或用于筛选数组中的匹配项
除此之外,您忘记了量词+
if (preg_match('/\/games\/[a-zA-Z0-9_]+/', $array))
{
// Add to DB
}
/\/games\/[a-zA-Z0-9_]+/
也可能受试者开始锚定
^
和结束锚定。和\w
可以取代您的字符列表。也应该匹配而不使用+
。这不是问题所在。如果您只需要部分字符串匹配,那么原始问题的正则表达式就完全足够了。是的,不相关。我假设与^
和$
完全匹配。但是OPs的主要问题可能是变量的使用(并且禁用了错误报告
)。