找到'内的模式长串

时间:2014-12-06 09:48:17

标签: php regex

我对我尝试做的某些模式有疑问......这是我的代码:

$test = 'this is a simply test';

preg_match_all("/^this is a [a-zA-Z] test$/", $test, $op_string);

print_r($op_string);

我一直在为这些家伙努力,但这并不能正常运作。这应输出:simply

模式必须包含与$test相同的字符串(我的意思是......它只能包含[a-zA-Z],因为我们需要更准确地找到它可能的)。

非常感谢你!

2 个答案:

答案 0 :(得分:2)

使用量词+匹配1个或多个字母:

$test = 'this is a simply test';
preg_match_all('/^this is a [a-zA-Z]+ test$/', $test, $op_string);

您使用的[a-zA-Z]仅匹配单个字母

答案 1 :(得分:1)

试试这个:

$re = "/^this is a ([a-zA-Z\\s]+) test$/m";
$str = "this is a simply test";

preg_match_all($re, $str, $matches);
var_dump($matches[1]);  // here you get match word or word set

live demo

<强>输出:

array (size=1)
  0 => string 'simply' (length=6)
相关问题