开发者

php regexp to variable

How I can write regexp to variable in regular expression?

$string = 'Test regular expression';
preg_match('#test (^\s+) expression#is', $string, $b);

//$b[1] = 'regular'; // But I need another way. 

I want to get

$b开发者_如何学编程['string'] = 'regular'; // Not using $b['string'] = $b[1];

Maybe

$regex = '#test (^\s+)/string/ expression#is';

Maybe there's a way to write a regular expression into an array variable in the regular expression?

Thank you, I hope you understand me.


You can do this with named capturing expressions:

preg_match('#test (?P<string>[^\s]+) expression#is', $string, $b);
print_r($b);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜